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

Greetings fellow perlmonks,
I was just wondering a quick question, possibly I might have asked this on the chatterbox too, but I'm lazy ,)
So here is some example code:
#!/usr/local/bin/perl -w $++; open(PIN,"/bin/ping -n -c 1 bat.cs.hut.fi|") or die "bug: $!"; $_ = <PIN>; # discard first line $_ = <PIN>; print $1 if /time=(\d+)/; close(PIN);
And my question goes, is there a way to discard (n) lines of output if I need just one?
I mean a kind of a "seek" for pipe-filehandles?
And yes I've thought about while loop here, but let's assume that I'd like to read just a certain line from a command that first prints out something and then prints out more that I just want to discard, then prints out stuff that I want again, so in the end I'd be going through 600 lines of text just to find (with a regexp) 2 lines, although I allready know their positions (err, line numbers)?
Also if I read the whole thing to an array and I would allready want to use the line that was close to the start, do I have to wait that the whole output of the pipe is in the array?
The way that I assume that @ping = <PIN> in the example would work is that perl reads the whole output of the program first to the array and then allows me to twiddle with it.
btw don't pay too much attention to the example code it was just the first thing that popped() to my twisted mind ,)

Replies are listed 'Best First'.
Re: Just a quickie about FH:s
by blakem (Monsignor) on Dec 12, 2001 at 01:21 UTC
    The other answers are probably the way you should go... However, if you just want to discard the first $n lines, you could do something like this:
    <FH> for 1..$n;

    -Blake

Re: Just a quickie about FH:s
by Zaxo (Archbishop) on Dec 11, 2001 at 23:57 UTC

    Here's one way if you know the line numbers are 7 and 11:

    $ perl -e 'open FH,"soversions.mk";print( (<FH>)[6,10])' all-sonames+=libdl=libdl.so$(libdl.so-version) all-sonames+=libresolv=libresolv.so$(libresolv.so-version)
    The (<FH>)[6,10] construct is a slice over the array of lines in the file. They are offset to allow for the usual convention for line counting in a file.

    After Compline,
    Zaxo

      Thanks, this was exactly what I was looking for ,)
      Funny tho that I didn't find any documentation about this even as I looked though all manpages and my perl bibles...
Re: Just a quickie about FH:s
by indapa (Monk) on Dec 12, 2001 at 00:01 UTC
    If I understand your question, you want to read/display a particular line in a file. There is a recipie (8.8) for this in the Perl Cookbook
    ($filename, $line) = @ARGV; open (IN, "$filename" ) or die "can't open $filename: $!\n"; while (<IN>) { $input = $_; last if $. == $line; } if ($. != $line) { die "didn't find line: $line in $filename\n"; } print $input;
    If your file isn't too large, read it into an array:
    @lines = <FILE>; $desired_line = $lines[$LINE_NUMBER];