[EMAIL PROTECTED] wrote:
>
Is there a quick and easy way that I can get the last key from a file.
For example, If I have this in my file (see below) how can I get the
line <val2>20<\val2>, with out reading through the entire file
<val1>0<\val1>
<val2>0<\val2>
<val3>0<\val3>
<val1>10<\val1>
<val2>10<\val2>
<val3>10<\val3>
<val1>20<\val1>
<val2>20<\val2>
<val3>20<\val3>
Have you tried reading the file forwards and simply recording the last
value found? Unless that approach is too slow it is the way you should
go.
If you need to optimise, then I suggest you make use of the
File::ReadBackwards module. Something like the program below will do
what you need.
HTH,
Rob
use strict;
use warnings;
use File::ReadBackwards;
my $fh = File::ReadBackwards->new('datafile') or die $!;
while (defined (my $line = $fh->readline)) {
if ($line =~ /<val2>(\d+)</) {
print $1, "\n";
last;
}
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/