loan tran wrote:
I would like to know if there is any way to process a
file using perl to get rid of extra records based on a
key value.
My key is the 1st field and in my output file I only
want 1 record per key and it has to be the last record
in the file.
So for example here is my file:
1 5 4
1 0 2
1 2 2
2 0 2
3 0 3
3 4 6
And here is the output file I want:
1 2 2
2 0 2
3 4 6
This will do what you want:
#!/usr/bin/perl
use warnings;
use strict;
my %last;
while ( <DATA> ) {
my $key = ( split )[ 0 ];
if ( not exists $last{ $key } ) {
print values %last;
%last = ( $key, $_ );
}
elsif ( eof DATA ) {
print;
}
else {
$last{ $key } = $_;
}
}
__DATA__
1 5 4
1 0 2
1 2 2
2 0 2
3 0 3
3 4 6
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>