skip first N lines when reading file

J

Jie

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
}
}
 
G

Gunnar Hjalmarsson

Jie said:
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;
 
P

Peter Makholm

Jie said:
======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
 
M

Mirco Wahab

Gunnar said:
Jie said:
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.
 
J

Jie

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
 
B

Brad Baxter

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
 
P

Peter Makholm

Big and Blue said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top