On 06 Nov 2000 11:17:47 GMT,
Quote:
> I'm trying to read the last line of a file only, using print, any ideas ?
You don't use print to read a line from a file. You use readline, or the
diamond operator.
If you're on Unix
my $last_line = `tail -1 $file`;
Otherwise:
open(FILE, $file) or die $!;
# If the file is small
my $last_line = (<FILE>)[-1];
# or
my ($last_line) = reverse <FILE>;
# If the file is larger:
my $last_line;
while(<FILE>) { $last_line = $_ }
# If the file is large
use File::ReadBackwards;
my $fbw = File::ReadBackwards->new($file) or die $!;
my $last_line = $fbw->readline;
or you can write stuff with seek and such that does what the
abovementioned module does.
maybe this should be added to the FAQ.
Martien
--
Martien Verbruggen |
Interactive Media Division | For heaven's sake, don't TRY to be
Commercial Dynamics Pty. Ltd. | cynical. It's perfectly easy to be
NSW, Australia | cynical.