in reply to Out of memory problem when copying contents of file into array
Here is a version based on TedYoung's but using a circular buffer which I think is a little more efficient. Need to find a really big file to do some benchmarks.
#!/usr/bin/perl use strict; use warnings; sub tailFile { my ($file, $lines) = @_; open F, $file or die $!; my @lines; $#lines=($lines-1); my $i; while (<F>) { $lines[$i++] = $_; $i = 0 if $i == $lines; } close F; # this works but not sure if this is safe using pre-dec # and var on same line see perlmonks nodes passim return @lines[$i--..$#lines, 0..$i] } print (tailFile ("/etc/passwd", 10));
Cheers,
R.
|
|---|