Monday, January 3, 2011

perl: read the last line of a file

METHOD 1:

open(TXT, "D:/latest/cities.txt");
my @lastarray = <TXT>;
print $lastarray[-1],"\n";


METHOD 2:
use Fcntl;
use Tie::File;

#-- open the file in read-only mode if you won't be modifying the file
tie my @rows, 'Tie::File', 'D:/latest/cities.txt', mode => O_RDONLY or die "error: $!\n";

#-- print last line
print $rows[-1];

No comments: