Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.

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


In reply to Re: read multiple lines by johngg
in thread read multiple lines by sharan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found