Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Skip 21 Lines

by qball (Beadle)
on Apr 24, 2001 at 18:49 UTC ( #75074=perlquestion: print w/ replies, xml ) Need Help??
qball has asked for the wisdom of the Perl Monks concerning the following question:

How do I skip the first 21 lines of a file and then start processing the remaining lines?

qball~"I have node idea?!"

Comment on Skip 21 Lines
Re: Skip 21 Lines
by arturo (Vicar) on Apr 24, 2001 at 18:52 UTC

    Gee, if only computers could count!

    for (my $i = 0; $i < 21; $i++) { <FILEHANDLE>; } # now read
Re: Skip 21 Lines
by suaveant (Parson) on Apr 24, 2001 at 18:52 UTC
    $. holds the input line number, so you could check against that...

    or you could keep a count... next unless $i++ > 21;
                    - Ant

Re: Skip 21 Lines
by davorg (Chancellor) on Apr 24, 2001 at 18:54 UTC
    while (<FILE>) { unless (1 .. 21) { # process the line - which is in $_ } }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Skip 21 Lines
by greenFox (Vicar) on Apr 24, 2001 at 18:55 UTC
Re: Skip 21 Lines
by merlyn (Sage) on Apr 24, 2001 at 18:59 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?

      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

Re: Skip 21 Lines
by Beatnik (Parson) on Apr 24, 2001 at 19:16 UTC
    Something I always use for the xth line of a file....
    (undef,undef,undef,undef,$line) = <FILE>;
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Something I always use for the xth line of a file....
      (undef,undef,undef,undef,$line) = <FILE>;
      At the expense of reading the entire file and tossing all but one of the lines. Ouch!

      -- Randal L. Schwartz, Perl hacker

        usually it's the 3rd or 4th line... :)

        Greetz
        Beatnik
        ... Quidquid perl dictum sit, altum viditur.
        Isn't it more maintainable to write that as:
        $line = (<FILE>)[4];
        ?
Re: Skip 21 Lines
by qball (Beadle) on Apr 24, 2001 at 19:36 UTC
    You all have great comments. How would I incorporate any one of those code snippets into the following code:
    open (DATA, "<file.txt") or die "Can't open file $!\n"; my @DATA = <DATA>; close (DATA); foreach $rec (@DATA) { chomp $rec; ($var1, $var2, $var3, $var4) = split(/,/,$rec); $var1 =~ s/\s+$//g; $var1 =~ s/"//g; $var2 =~ s/\s+$//g; $var2 =~ s/"//g; $var3 =~ s/\s+$//g; $var3 =~ s/"//g; $var4 =~ s/\s+$//g; $var4 =~ s/"//g; $compHash{$var1} = $var2; $aliasHash{$var3} = $var1; }

    This code works great, just needs to skip the first 21 lines.

    qball~"I have node idea?!"
      open (DATA, "<file.txt") or die "Can't open file $!\n"; <DATA> while $. < 21; my @DATA = <DATA>; close (DATA);
        Nice use of an obscure variable (the obfuscator in me salutes you), but you'd better hope that the file is at least 22 lines long, or you'll be waiting quite a while for your data :-)
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
      You are repeating quite a bit of code there :) Try this (partially tested)-
      my $file = 'file.txt'; # open for input is default and now our die tells us which # file we were trying to open open (DATA, $file) or die "Can't open $file $!\n"; # assuming you aren't using @DATA for something else later # there is no need to slurp it all into memory now while (<DATA>) { next unless $. > 21; chomp; my @temp=(); foreach (split /,/){ s/"//g; # strip the quotes first if you want s/\s+$//; # to remove trailing whitespace push @temp, $_; } $compHash{$temp[0]} = $temp[1]; $aliasHash{$temp[2]} = $temp[3]; # you did mean $var4? } close (DATA);

      I am sure it could be reduced further with a judicious use of map but I can't get my head around it this morning :(

      Since you are parsing quoted CSV data you may want to look at Text::CSV which is a standard module or tilly's Text::xSV which is also available from CPAN.

      --
      my $chainsaw = 'Perl';

Re: Skip 21 Lines
by satchboost (Scribe) on Apr 24, 2001 at 22:39 UTC
    Ummm .... why not use seek? Something like:
    open MY_FILE, $filename; seek MY_FILE, 21, 0; while (<MY_FILE) { ... do reading schtuff here ... }

    That way, you go right to where you want to go. No silly while loops or anything like that.

        whoops. I'm a dunce! That'll teach me to think I can see something easy that 4 monks before me didn't.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-07-14 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (258 votes), past polls