in reply to Best way to read line x from a file
I think that Tie::File is the best compromise between speed and simplicity for such tasks.
The fastest way would be a loop like the following, assuming that the line indicated by $line_no will start in the first half of the file:
<FILE> while ($line_no--); my $line2 = <FILE>;
as then, Perl and the OS will do some buffering for you, and you don't read the whole file for nothing.
If your file size is smaller than one sector, the OS (and the HD) will read it into memory anyway, and it might be faster to slurp it into memory and use a crafted regular expression against it:
use File::Slurp qw(slurp); my $f = slurp $filename; my $line2 = $1 if (m!\n{$line_no-1}([^\n]*)\n!sm);
So in the end, you will have to benchmark a lot.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re2: Best way to read line x from a file
by Hofmator (Curate) on Mar 30, 2004 at 12:17 UTC |