Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Extracting Text Lines

by mikevanhoff (Acolyte)
on Jul 31, 2003 at 01:24 UTC ( [id://279437]=perlquestion: print w/replies, xml ) Need Help??

mikevanhoff has asked for the wisdom of the Perl Monks concerning the following question:

I want to thank everyone for their help. I think I have figured out. The following code searches a test file for a phrase, when it finds it, it reads in the very next line, and prints it out. Then the next 2 lines are skipped, and finally the last line is printed. I am posting it here for review, and would appreciate any comments. I would like to note my use of the "readline" function. According to the docs I found, it only works from within a while loop. Anyway Thanks again.
$success = 0; open(FILE,"dumptape.log") or die ("Could not open file"); while(<FILE>) { if (/Dump phase number 3/) { while (<FILE>) { readline(<FILE>); print; <FILE> ; } # print; $success = 1; last; } } if(!$success) { print "Failed dump log\n"; #do other failed dump log stuff here } close FILE;

Replies are listed 'Best First'.
Re: Extracting Text Lines
by tedrek (Pilgrim) on Jul 31, 2003 at 03:44 UTC

    That code doesn't do what you say it does. It searches through the file then prints the line after your target line, and every 3rd line after that till the end of the file. The reason is that the inner while loop continues until the file ends. Also the readline doesn't do what you mean for it to do, it actually reads a line from the file (<FILE>) then uses that as a argument to readline. If you had used warnings it would have complained about a readline on unopened filehandle. readline(FILE)is the same as <FILE> and is not restricted to a while loop so where ever you read that is wrong. Here is a way over commented piece of code that does what you say you want :)

    use warnings; use strict; my $success = 0; open(FILE, "<", "dumptape.log") or die "Couldn't open dumptape.log: $!"; while (<FILE>) { if (/Dump phase number 3/) { # We found the line my $line = <FILE>; # read the next line print $line; # print the next line <FILE>; # Skip a line <FILE>; # Skip a line $line = <FILE>; # read the fourth line print $line; # print the fourth line $success = 1; # we found it last; # So we're done! } } close FILE; if (! $success) { print "Failed dump log\n"; }
      my $line = <FILE>; # read the next line print $line;
      I think these two lines are only needed because you want to provide a scalar context to <FILE>. That can be done easier and more efficiently (and probably more readable as well):
      print scalar <FILE>; # read the next line and print it
      Liz
Re: Extracting Text Lines
by talexb (Chancellor) on Jul 31, 2003 at 02:20 UTC

    It seems to me that you could simplify the code like this.

    open(FILE,"dumptape.log") || die ("Could not open file: $!"); my $success; for ( $success = 0; $success != 1 && <FILE>; ) { if ( /Dump phase number 3/ ) { print <FILE>; $success = 1; } } close ( FILE ); if ( !$success ) { print "Failed dump log\n"; #do other failed dump log stuff here }

    Then again, maybe checking for success every time is overkill -- the last that you had in there works fine.

    --t. alex
    Life is short: get busy!
      print <FILE>;
      This wont do as you expect it to do. print puts is argument in list context, and a file handle in list context returns all the lines in the files. Thus you would print the remainder of the file after having found your line.

      T I M T O W T D I
Re: Extracting Text Lines
by mildside (Friar) on Jul 31, 2003 at 03:08 UTC
    I think your use of readline() is redundant since <FILE> and readline(FILE) are equivalent. So readline(<FILE>) is actually reading the line using <>, and not readline().

    Cheers!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found