Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Skipping the first 4 lines of a file:: Is there a better way?

by PerlBear (Hermit)
on Jun 03, 2005 at 12:23 UTC ( #463234=perlquestion: print w/ replies, xml ) Need Help??
PerlBear has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Fellow Monks!
In a nutshell what I would like to do is skip the first 4 lines of a data file that I am reading in. Essentially the first 4 lines are junk, header info that I don't need.
Basically I already do know how to do this; I have supplied my two approaches to the task below, both of which work just fine. I am just wondering if there is an even better way to do it, perhaps a one-liner.... (always looking for those efficient little one liners) :) :) :)
the code:
#!/usr/bin/perl -w use strict; use FileHandle; my $infile="T:/data.dat"; open(IN,"$infile") or die "CANNOT OPEN $infile !!\a\n"; my $c=0; while ($c<4){ my $junk=<IN>; $c++; }
..or perhaps even less elegant...
#!/usr/bin/perl -w use strict; use FileHandle; my $infile="T:/data.dat"; open(IN,"$infile") or die "CANNOT OPEN $infile !!\a\n"; my $junk=<IN>; $junk=<IN>; $junk=<IN>; $junk=<IN>;
I am basically curious what other perhaps better ways that this can be done.
As always, your thoughts, bits of wisdom, and insight are greatly appreciated!
Thanks in advance!

Comment on Skipping the first 4 lines of a file:: Is there a better way?
Select or Download Code
Re: Skipping the first 4 lines of a file:: Is there a better way?
by Joost (Canon) on Jun 03, 2005 at 12:26 UTC
Re: Skipping the first 4 lines of a file:: Is there a better way?
by mrborisguy (Hermit) on Jun 03, 2005 at 13:07 UTC

    Something like your first, but in a for loop:

    for ( 1..4 ) { <IN> }

    But really what I would do:

    <IN> for ( 1..4 );

        -Bryan

Re: Skipping the first 4 lines of a file:: Is there a better way?
by sh1tn (Priest) on Jun 03, 2005 at 14:15 UTC
    If the size of the file is not a concern:
    ... my $skip = 4; { open FH, "filename"; local@_ = <FH>; close FH; @_ = split /(?:.+\n){$skip}/m, "@_"; for( @_ ){ # work without first four lines } } ...


      @_ = split /(?:.+\n){$skip}/m, "@_";

      Gah, that's terribly inefficient. splice( @_, 0, $skip ) at the least if you feel you have to modify @_. foreach( @_[  $skip .. $#_ ] ) { ... } would be even better since it doesn't have to move anything around.

      Update: Changed from hardcoded 4 to $skip to match.

      --
      We're looking for people in ATL

        Gah, that's terribly inefficient.

        It's a competition for efficiency?
        What's so inefficient? 1 millisecond?


      Efficiency aside, does this really work? Okay, well, I know the answer: it doesn't. There is no limit on the split, and the delimiter is any {4} lines.

      So, if my file is 10 lines long, I get two empty strings, then the remaining 2 lines. Infact, you only ever end up with the last X lines of your file (where X = lines % $skip), preceeded with (lines div $skip) empty strings.

      @_ = split /(?:.+\n){$skip}/m, "@_", 2; # add limit # closer, but now I have @_ == 2, # since there is a leading null shift @_; # now I have a single element array with # the file remainder in $_[0] @_ = split /\n/, $_[0]; # now I have the desired results.

      I agree that solutions don't always need to be the most efficient, but parsing through the entire file at least three times is O(n) where O(1) is available in a single line of code.

Re: Skipping the first 4 lines of a file:: Is there a better way?
by djohnston (Scribe) on Jun 03, 2005 at 16:16 UTC
    If slurping were an option, I'd do it like this:

    local (undef, undef, undef, undef, @_) = <IN>;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-09-23 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls