Perl Monk, Perl Meditation | |
PerlMonks |
Re: grep using regexby leslie (Pilgrim) |
on Mar 02, 2009 at 05:09 UTC ( [id://747385]=note: print w/replies, xml ) | Need Help?? |
Hi xing, Your code is working fine with this given input. But you need to use flip-flop operator very carefully. For example, if there is a file and we want to process only certain sections of it, then using the flip-flop operator the code can be written like:
Each time around the loop, the current line is checked by the flip-flop operator. If the line doesn't match /!! START !!/ then the operator returns false and the loop continues. When we reach the first line that matches /!! START !!/ then the flip-flop operator returns true and the code in the if block is executed. On subsequent iterations of the while loop, the flip-flop operator checks for matches again /!! END !!/, but it continues to return true until it finds a match. This means that all of the lines between the "!! START !!" and "!! END !!" markers are processed. When a line matches /!! END !!/ then the flip-flop operator returns false and starts checking against the first regex again.
In Section
Seekers of Perl Wisdom
|
|