Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: read multiple lines

by johngg (Canon)
on Nov 13, 2008 at 13:34 UTC ( [id://723439]=note: print w/replies, xml ) Need Help??


in reply to read multiple lines

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.

use strict; use warnings; sub sep { print q{=} x 50, qq{\n}; } sep(); my $dataOffset = tell DATA; my $printIt = 0; while( <DATA> ) { if( m{begin} ) { $printIt = 1; next; } elsif( m{^end process$} ) { $printIt = 0; next; } else { print if $printIt; } } sep(); seek DATA, $dataOffset, 0; $printIt = 0; while( <DATA> ) { if( m{^begin} ) { $printIt = 1; next; } elsif( m{^end process$} ) { $printIt = 0; next; } else { print if $printIt; } } sep(); seek DATA, $dataOffset, 0; $printIt = 0; while( <DATA> ) { if( m{^begin$} ) { $printIt = 1; next; } elsif( m{^end process$} ) { $printIt = 0; next; } else { print if $printIt; } } sep(); __END__ hfkjd process jhk do not begin here because we do not want this line or this start begin *** This is a line we want *** *** So is this *** end process scrap begin the beguine hkl process khfk shoot hooter begin *** Another desired piece of data *** end process more dross

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.

================================================== because we do not want this line or this start *** This is a line we want *** *** So is this *** hkl process khfk shoot hooter *** Another desired piece of data *** ================================================== *** This is a line we want *** *** So is this *** hkl process khfk shoot hooter *** Another desired piece of data *** ================================================== *** This is a line we want *** *** So is this *** *** Another desired piece of data *** ==================================================

I hope I have guessed correctly and you will find this helpful.

Cheers,

JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://723439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found