How to directly seek to a particular line number while reading a file in Perl

J

jeniffer

I want to directly seek to a particular line in an opened file and
ensure that the reading of the file starts from there only.The lines in
the file are of random length and so seek cannot be used.Presently i am
looping from the start ,maintaining a counter and checking whether
desired line has reached.
 
K

Kraven

jeniffer said:
I want to directly seek to a particular line in an opened file and
ensure that the reading of the file starts from there only.The lines in
the file are of random length and so seek cannot be used.Presently i am
looping from the start ,maintaining a counter and checking whether
desired line has reached.

perldoc Tie::File
 
S

Sisyphus

..
..
Presently i am
looping from the start ,maintaining a counter and checking whether
desired line has reached.

You don't need to maintain a counter. Perl has the special variable called
"$." which does that for you. See 'perldoc perlvar'.

Cheers,
Rob
 
C

Charles DeRykus

jeniffer said:
I want to directly seek to a particular line in an opened file and
ensure that the reading of the file starts from there only.The lines in
the file are of random length and so seek cannot be used.Presently i am
looping from the start ,maintaining a counter and checking whether
desired line has reached.

Actually random length lines aren't a problem for seek. Capturing the
line start postions can easily be done and will be very fast. On the
other hand. if the file is small or speed isn't important, Tie::File
which was already mentioned, is a better alternative.

Here's a program which prints itself by capturing line starts:

#!/usr/bin/perl

use strict; use warnings;

my @lines = ( 0 );
seek *DATA, 0, 0 or die $!;
push @lines, tell() while <DATA>;

foreach my $pos ( @lines ) {
seek *DATA, $pos, 0 or die $!;
print scalar <DATA>;
}
__END__


hth,
 
X

xhoster

jeniffer said:
I want to directly seek to a particular line in an opened file and
ensure that the reading of the file starts from there only.The lines in
the file are of random length and so seek cannot be used.Presently i am
looping from the start ,maintaining a counter and checking whether
desired line has reached.

So what is the problem with that method? Too slow? Too error prone?
What is your question?

If not worried about portability, I'd probably do this:

open my $fh, "tail +$start_line file | " or die $!
while (<$fh>) {
#...
};
close $fh or die $!;

Xho
 
J

jl_post

jeniffer said:
I want to directly seek to a particular line in an opened file and
ensure that the reading of the file starts from there only.The lines in
the file are of random length and so seek cannot be used.Presently i am
looping from the start ,maintaining a counter and checking whether
desired line has reached.


Dear Jeniffer,

If you want to use a conventional while-loop to loop through the
lines of your file, you can always make use of the $. variable (read
more about it in "perldoc perlvar"). For example, let's say you want
to print out every line in uppercase of the file starting with the
tenth line, you can do something like this:

while (<IN>)
{
next if $. < 10; # skip all lines before the tenth one
print uc($_); # print line in upper-case
}

I hope this helps, Jeniffer.

-- Jean-Luc
 
A

axel

Actually random length lines aren't a problem for seek. Capturing the
line start postions can easily be done and will be very fast. On the
other hand. if the file is small or speed isn't important, Tie::File
which was already mentioned, is a better alternative.
Here's a program which prints itself by capturing line starts:
#!/usr/bin/perl

use strict; use warnings;

my @lines = ( 0 );
seek *DATA, 0, 0 or die $!;
push @lines, tell() while <DATA>;

foreach my $pos ( @lines ) {
seek *DATA, $pos, 0 or die $!;
print scalar <DATA>;
}
__END__

It seems to me that this takes longer to run than a program which
uses $. since the data is read twice.

Axel
 

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