skip first N lines when reading file

Discussion in 'Perl Misc' started by Jie, Jun 27, 2007.

  1. Jie

    Jie Guest

    Hi,

    can someone please let me know how to skip first N lines when reading
    a file in perl? I know that I can write something like below, but too
    much coding and computing is involved.

    also, can someone please let me know if there is a master pdf file
    that has all the perl documentation content. right now, from
    perldoc.per.org i found it is divided into many many files..

    ======my code to skip first N lines======

    $line = 0;
    while(<IN>) {
    if ($line <= N) {
    } else {
    do something
    }
    }
     
    Jie, Jun 27, 2007
    #1
    1. Advertising

  2. Jie wrote:
    > can someone please let me know how to skip first N lines when reading
    > a file in perl?


    One way:

    use Tie::File;
    my $skip = 3;
    tie my @file, 'Tie::File', 'myfile' or die $!;
    print "$_\n" for @file[ $skip..$#file ];
    untie @file;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 27, 2007
    #2
    1. Advertising

  3. Jie <> writes:

    > ======my code to skip first N lines======
    >
    > $line = 0;
    > while(<IN>) {
    > if ($line <= N) {
    > } else {
    > do something
    > }
    > }


    My take:

    while(<IN>) {
    next if 1 .. N;

    do something;
    }

    if N is a constant expression or else you have to use at little less
    magic:

    my $N = lines_to_skip();
    while(<IN>) {
    next if 1 .. $N==$.;

    do something;
    }

    Read 'perldoc perlop' look for the 'Range Operators' section.

    //Makholm
     
    Peter Makholm, Jun 27, 2007
    #3
  4. Jie

    Mirco Wahab Guest

    Gunnar Hjalmarsson wrote:
    > Jie wrote:
    >> can someone please let me know how to skip first N lines when reading
    >> a file in perl?

    >
    > One way:
    >
    > use Tie::File;
    > my $skip = 3;
    > tie my @file, 'Tie::File', 'myfile' or die $!;
    > print "$_\n" for @file[ $skip..$#file ];
    > untie @file;


    This might be sometimes a good, sometimes a bad idea.
    example: file w/110000 lines (4.xMB)

    The tie-solution

    ...
    use Tie::File;
    my ($skip, $len) = (30, 0);

    tie my @file, 'Tie::File', 'myfile.txt' or die $!; # invoke file
    print $file[$skip], "\n"; # skip n lines, print this line (validity)

    $len += length for @file[ $skip..$#file ]; # compute lentgh of remaining lines

    print scalar @file, " => $len\n"; # output: length and number of lines

    untie @file;
    ...

    would need around 16 seconds to (user) complete (588/Linux/2.5GHz AthlonXP),
    whereas the straight thing:

    ...
    my ($skip, $len) = (30, 0);

    open my $fh, '<', 'myfile.txt' or die $!; # invoke file
    1 while $skip-- && <$fh>; # skip n lines
    print scalar <$fh>, "\n"; # print this line (validity)

    $len += (length)-1 while <$fh>; # compute lentgh of remaining lines
    # (remove newline!)
    print "$. => $len\n"; # output: length and number of lines
    ...

    passes through in far below 0.15 (!) seconds on the same machine
    (several runs checked)

    Regards

    M.
     
    Mirco Wahab, Jun 27, 2007
    #4
  5. Jie

    Jie Guest

    Hi, Makholm:

    Thank you so much!! "next if 1 .. N" works great!!

    I am going to post another question here, regarding an "out of memory"
    issue which has troubled me for a long while. Hope I can get it
    resoved here.

    Thank you again!!

    Jie
     
    Jie, Jun 27, 2007
    #5
  6. Jie

    Brad Baxter Guest

    On Jun 27, 11:59 am, Jie <> wrote:
    > Hi,
    >
    > can someone please let me know how to skip first N lines when reading
    > a file in perl? I know that I can write something like below, but too
    > much coding and computing is involved.
    >
    > also, can someone please let me know if there is a master pdf file
    > that has all the perl documentation content. right now, from
    > perldoc.per.org i found it is divided into many many files..
    >
    > ======my code to skip first N lines======
    >
    > $line = 0;
    > while(<IN>) {
    > if ($line <= N) {
    > } else {
    > do something
    > }
    >
    > }


    Too much coding? Do I hear that right?

    perl -ne'BEGIN{$n=4}print if$.>$n' file

    perl -pe'INIT{$n=4}$_=""if$.<=$n' file

    --
    Brad
     
    Brad Baxter, Jun 28, 2007
    #6
  7. Big and Blue <> writes:

    >> while(<IN>) {
    >> next if 1 .. N;
    >>
    >> do something;
    >> }

    >
    > Why bother doing the test once you've skipped?
    >
    > while (<IN>) { last if ($. == $skip};
    > while (<IN>) {
    > ...code to run after $skip lines...
    > }


    It's a matter of style and preferences. In general I prefere to
    iterate over a file in one loop. Starting in the middle of a data
    structure is a special case I thinks adds an amount of complexity to
    the code.

    In general I often write code like:

    while(<IN>) {
    last if end_condition_1;
    last if end_condition_2;

    next if skip_condition_1;
    next if skip_condition_2;

    if (case1) {
    handle_case_1;
    } elsif (case2) {
    handle_case_2;
    } else {
    handle_default_case;
    }
    }

    Even in the simple case where the skip condition is 'the first N
    lines' I stick to this scheme. The added cost is negligible in my
    common case. Other peoples problems may need another cost benefit
    analysis of course.

    Why I use the range operator and not just "$. < N" is more obscure. I
    just like using the range operator.

    //Makholm
     
    Peter Makholm, Jun 28, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FrancisC
    Replies:
    7
    Views:
    478
    Howard
    Oct 10, 2003
  2. FrancisC
    Replies:
    16
    Views:
    759
  3. puzzlecracker

    skip blank lines

    puzzlecracker, Oct 16, 2005, in forum: C++
    Replies:
    3
    Views:
    602
    Jim Langston
    Oct 17, 2005
  4. Brian Adkins
    Replies:
    30
    Views:
    306
    Pit Capitain
    Oct 30, 2007
  5. Replies:
    5
    Views:
    235
    Nico Coetzee
    Jul 8, 2005
Loading...

Share This Page