XP is just a number | |
PerlMonks |
Resetting a flip-flop operatorby AppleFritter (Vicar) |
on Aug 06, 2015 at 10:52 UTC ( [id://1137648]=perlquestion: print w/replies, xml ) | Need Help?? |
AppleFritter has asked for the wisdom of the Perl Monks concerning the following question: Greetings, esteemed monks! Allow this humble pony to drink the sweet nectar of knowledge from the font of your collective wisdom. (Or alternatively, how 'bout some hard cider?) I need to read a number of files. In each file, each line holds a piece of data, or a marker indicating the beginning or end of a section; I'm interested only in data in a specific section. Normally, I'd do something like this:
However, it turns out that in these log files, the section end marker may be omitted if there is no following section: the end of the file itself indicates the end of the section then. This wreaks havoc with the above logic, as the flip-flop operator, not having seen the marker, still evaluates to true when the outer loop moves on to the next file, and wrongly causes lines before the start marker in that file to be processed. Of course it would be trivial to add a flag indicating whether I'm in the right section, and reset that for each file. But doing that would essentially manually emulate the flip-flop operator, which strikes me as less than elegant. So I'm wondering -- is there a way to "reset" the flip-flop operator, as it were, so that it starts returning false again at the beginning of each new file?
And two sample files (say log1.txt and log2.txt):
And:
If you pass these in in this order, you'll get:
And as you can see, the uninteresting lines from before the PP section in the second file get included in the output.
Back to
Seekers of Perl Wisdom
|
|