in reply to print a determinate line number

check the ***magic*** $., man perlvar

HANDLE->input_line_number(EXPR) $INPUT_LINE_NUMBER $NR $. Current line number for the ***last filehandle*** acces +sed.

Actually it's a bit more magic than you might like, in which case doing it yourself might be safer when using multiple input handles in a program.

Replies are listed 'Best First'.
Re^2: print a determinate line number
by almut (Canon) on Oct 06, 2009 at 10:58 UTC
    check the ***magic*** $.

    Looks like the OP is already using $.  So I can only guess that the question is how to directly jump to a certain line in a file without going through the content before that line... in which case my reply would be that this could only be done if the lines all have the same length...  Kinda speculative, of course.

      That would require e.g. seek(), which can be way faster. If the data format allows to use it.

      Maybe that's idiom the opener missed (line-by-line reading of input)

      while(<>){ # <>: used handle is STDIN print if $.==5; # so $. is that handle's line number }

      or do it yourself in say $i

      my $file="/etc/hosts"; my $i=0; # let's use 1-based numbers open(FH,"<",$FILE) or die "dying on $FILE"; while(<FH>){ $i++; print "$i: $_" if $i == 5; }