Problems? Is your data what you think it is? | |
PerlMonks |
Re: read multiple linesby johngg (Canon) |
on Nov 13, 2008 at 13:34 UTC ( [id://723439]=note: print w/replies, xml ) | Need Help?? |
I am going to hazard a couple of guesses here. First guess, the data you want to print between "begin" and "end process" might not be a simple as you describe and might not be easily identified by a regex pattern. Second guess, from your code it looks like you might be having a stab at a simple state engine. Looking at the desired output I'm not sure why you are also trying to detect the "process ..." line. In the code below I read the DATA filehandle a line at a time and use the variable $printIt to keep track of the state that determines whether we print a line or not. Set to 0 (false) initially, it is set to 1 (true) when we find a line just containing begin and reset to false when we get end process. I have amended your data, making the desired output lines more random (if you ignore the asterisks I've put in to make them stand out :-) and adding some more lines to illustrate the importance of anchoring your patterns (have a look at perlretut and perlre). I make three passes over the data (have a look at readline, tell and seek), firstly processing with the pattern detecting "begin" not anchored at all, secondly with it just anchored to the beginning of the string and finally anchored at both ends.
Here is the output. Note how the lines printed are whittled down as we add the anchors into the regular expression that detects the begin line.
I hope I have guessed correctly and you will find this helpful. Cheers, JohnGG
In Section
Seekers of Perl Wisdom
|
|