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

Hello All: How can I ignore the first few (5) lines of a file? Thanks for the help!

Replies are listed 'Best First'.
Re: ignore first 5 lines of a file
by tachyon (Chancellor) on Apr 12, 2002 at 21:13 UTC

    $. gives you the current line you are reading from a file so:

    while (<FILE>) { next until $. > 5; # do stuff with lines 6..EOF }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: ignore first 5 lines of a file
by thelenm (Vicar) on Apr 12, 2002 at 21:34 UTC
    Another possible solution:
    <FILE> for 1..5; while (<FILE>) { # do stuff }
    Then you don't need to test $. on each iteration, but that's only a very small savings, if any.
Re: ignore first 5 lines of a file
by stephen (Priest) on Apr 12, 2002 at 22:19 UTC

    Just for sheer amusement value, here's another way of skipping the first five lines of a file:

    while (<>) { do_something($_) if (6..eof()); }

    Some explanation is in order. When the range operator .. appears in a scalar context, it becomes the flipflop operator, which behaves very differently and magically. If the argument on either side of it is a constant number, it does an implicit compare with $.. Another way of doing it would be:

    while (<>) { next if (1..5); # Whatever here }

    stephen

Re: ignore first 5 lines of a file
by abaxaba (Hermit) on Apr 13, 2002 at 07:03 UTC
    It's roundabout, but certainly basic and straightforward, which is a good thing, IMHO:
    @lines=<FILE>; for (my $i=5;$i<$#lines;$i++) { #stuff }
A reply falls below the community's threshold of quality. You may see it by logging in.