Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

read multiple lines

by sharan (Acolyte)
on Nov 13, 2008 at 09:22 UTC ( #723383=perlquestion: print w/ replies, xml ) Need Help??
sharan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have to write a prog to read some lines from data
hfkjd process jhk start begin output1 output2 end process scrap hkl process khfk shoot hooter begin output3 end process
i want output as
output1 output2 output3
my code looks like
if (/end process/) {end(); } if (/process/) {if(/begin/) { print $_; }}
But still am not able to get the desired output.. could u please help me... Many thanks in advance.

Comment on read multiple lines
Select or Download Code
Re: read multiple lines
by Anonymous Monk on Nov 13, 2008 at 09:36 UTC

      I knew this question seemed familiar: read multi lines of a file in perl -- well remembered brother Anonymous Monk.

      Same OP, too !

      For a moment there I thought this might be coursework. Looks as though the previous answers failed to grasp the subtlety of the problem ?

Re: read multiple lines
by GrandFather (Cardinal) on Nov 13, 2008 at 09:37 UTC

    Neither your code nor your description really tell us anything useful about what you are trying to achieve. Something as simple as:

    use strict; use warnings; /^output/ and print while <DATA>; __DATA__ hfkjd process jhk start begin output1 output2 end process scrap hkl process khfk shoot hooter begin output3 end process

    does what you ask for. But that sure doesn't seem likely to be the answer your teacher wants. Until you can explain clearly what it is that you need to do you will not be able to write a program to do it. First step: describe the problem.


    Perl reduces RSI - it saves typing
Re: read multiple lines
by Punitha (Priest) on Nov 13, 2008 at 09:51 UTC

    Hi

    If you want the content in between 'begin' and 'end process' means try like this,

    use strict; my $string ='hfkjd process jhk start begin output1 output2 end process scrap hkl process khfk shoot hooter begin output3 end process'; while($string=~/\bbegin((?:(?!end process).)*)end process/sg){ print "IN:$1\n"; }

    or tell us clearly what you really need and what you tried so far?

    Punitha

Re: read multiple lines
by johngg (Abbot) on Nov 13, 2008 at 13:34 UTC

    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
Node Status?
node history
Node Type: perlquestion [id://723383]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-08-22 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls