Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Skip 21 Lines

by merlyn (Sage)
on Apr 24, 2001 at 18:59 UTC ( [id://75081]=note: print w/replies, xml ) Need Help??


in reply to Skip 21 Lines

Good time to bring out that underused "flip-flop" operator:
while (<HANDLE>) { next if 1..21; ... rest of processing here ... }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Skip 21 Lines
by suaveant (Parson) on Apr 24, 2001 at 19:11 UTC
    That one is a weird one to get your head around... I never think of it. It is very cool, though. Of course, you could always do...
    while (<HANDLE>) { last unless 1..21; } ... rest of processing here ...
    to prevent that check running for the following lines, but that's just downright ANAL :)
                    - Ant
      last unless 1..21;

      Won't this do the opposite of what was intended and skip all but the first 21 lines of the file?

        No... the while goes through 21 one times, then breaks out... (tho might lose line 22... 1..20 might be needed in this case... but once it exits the loop you either start a new loop or read the reast into an array or whatever, since your placeholder in the file is now at line 22. You see? But that way it isnt perfoming the line check after the 21st line.. not a big efficiency savings... but a bit.
                        - Ant
Re: Re: Skip 21 Lines
by MeowChow (Vicar) on Apr 25, 2001 at 11:27 UTC
    Any reason you wouldn't use:
    <HANDLE> for 1..21; ...
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      You said:
      Any reason you wouldn't use:
      <HANDLE> for 1..21;
      Yes. Try that with <ARGV> (aka <>) when there's only 15 lines in the files specified on the command line. After it hits EOF, it now starts reading unexpectedly from STDIN for the remaining 5 lines (after 1 undef for the eof), because the process starts over!

      Moral: Always watch for an early EOF from a filehandle.

      -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found