Page 1 of 1

Restrict to when a global variable has a particular value?

Posted: Tue Dec 13, 2011 3:28 am
by dfhtextpipe
If one captures a pattern to a global variable, might it be feasible to subsequently use the captured value for a restrict filter?

Example: Suppose I capture this XML attribute early on in the file, then I'd know which "work" I'm dealing with.

Code: Select all

Comment...
|  Capture the value of osisWork
|
+--Restrict to between tags <work>...</work>
   |  [ ] Include text
   |  [X] Match case
   | Max size: 65536
   |
   +--Restrict to each line in turn
      |
      +--Perl pattern [osisWork="(.+)"] with []
         |  [X] Match case
         |  [ ] Whole words only
         |  [ ] Case sensitive replace
         |  [ ] Prompt on replace
         |  [ ] Skip prompt if identical
         |  [ ] First only
         |  [ ] Extract matches
         |  Maximum text buffer size 4096
         |  [X] Maximum match (greedy)
         |  [ ] Allow comments
         |  [ ] '.' matches newline
         |  [X] UTF-8 Support
         |
         +--Perl pattern ["(.+)"] with []
            |  [X] Match case
            |  [ ] Whole words only
            |  [ ] Case sensitive replace
            |  [ ] Prompt on replace
            |  [ ] Skip prompt if identical
            |  [ ] First only
            |  [ ] Extract matches
            |  Maximum text buffer size 4096
            |  [ ] Maximum match (greedy)
            |  [ ] Allow comments
            |  [ ] '.' matches newline
            |  [X] UTF-8 Support
            |
            +--Capture to variable @osisWork
                Reset: 1
                
Could I then have a subsequent subfilter that only takes certain actions depending on the value of the osisWork variable?

i.e. For a filter to be appled to several very similar works, but with some special cases applicable only to certain works.
Assume that all these works have the same set of input filenames.

Re: Restrict to when a global variable has a particular valu

Posted: Tue Dec 13, 2011 9:09 am
by DataMystic Support
Hi David,

Yes! You can actually use a Scripting filter to control its own subfilters, so you can perform any kind of conditional processing you like.

If you want to engage the subfilters of a Scripting filter for a small piece of text (say < 1 MB), then use:

Code: Select all

TextPipe.subFilterEntireText( inputText : string )
If you need the subfilters of a Scripting filter to process a huge set of data, then use

Code: Select all

initSubFilters()
subFilterChunk( inputString : string )
subFilterChunk( inputString : string )
...
flushSubfilters()
Here is a complete example from the help file:

Code: Select all

dim lineNumber

function processLine(line, EOL)

lineNumber = lineNumber + 1
if (lineNumber = 100) then
   'start the special processing on line 100
   TextPipe.initSubFilters
   TextPipe.subFilterChunk( line & EOL )
   processLine = ""
else if (lineNumber > 100) and (lineNumber < 200) then
   'special processing for lines 100..200
   TextPipe.subFilterChunk( line & EOL )
   processLine = ""
else if (lineNumber = 200) then
   'end special processing on line 200
   TextPipe.flushSubfilters
   processLine = line & EOL
else
   processLine = line & EOL
end if

end function

 
function endFile()
if (lineNumber = 100) then
   endFile = ""
else if (lineNumber > 100) and (lineNumber < 200) then
   TextPipe.flushSubfilters
   endFile = ""
else if (lineNumber = 200) then
   TextPipe.flushSubfilters
   endFile = ""
else
   endFile = ""
end if

end function


function startJob()
  lineNumber = 0
end function