A different approach would be to use seek to position the file pointer some number of bytes from the end of your file, 10,000 say, and then use read to read those last 10,000 bytes of the file into buffer held in a scalar variable. You could then open another filehandle on a reference to that scalar which would allow you to read the last 100-200 lines of your file (depending on line lengths) into an array without having to read the whole file. Something like (not tested):
open my $bigFH, q{<}, q{myBigFile} or die $!; seek $bigFH, -10000, 2; read $bigFH, my $last10k, 10000; open my $last10kFH, q{<}, \ $last10k or die $!; my @lastLines = <$last10kFH>;
Be aware the first line you read will most likely be a partial line and this approach will not help if you need to know specific line numbers; for that the while loop would be required.
I hope this is helpful.
Cheers,
JohnGG
In reply to Re: Large text files into arrays, accessing final elements
by johngg
in thread Large text files into arrays, accessing final elements
by isabella423
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |