Joseph Paish wrote:
> i have a data file that looks something like this :
> ( it is already sorted by date )
> 
> 12/22/02 abc 123 456 789
> 12/23/02 def 246 812 98234
> 12/24/02 ank 987 23456 8762
> 12/27/02 abc 987 345 65434
> 01/05/03 abc 876 2356 87
> 01/09/03 ank 875 234 98098
> 02/01/03 def 987 3453 456
> 02/05/03 ghi.th 987 2345 94
> 
> ------------
> 
> i need the final entry of each unique second field.  in other words,
> in the output file, i would have :
> 
> 01/05/03 abc 876 2356 87     << last abc entry
> 02/01/03 def 987 3453 456   << last def entry
> 01/09/03 ank 875 234 98098 << last ank entry
> 02/05/03 ghi.th 987 2345 94 << last ghi.th entry
> 
> 
> -----------
> 
> what i have done so far :
>      sort data file by the second field
>      while (not end of file) {
>           read each record and store it in a temporary array
>           keep reading until the second field changes
>           if the second field changes {
>                write the temporary array to the output file
>                store the "just read" record in the temporary array
>           }
>      }
> 
> 
> this seems needlessly complicated.  there has to be a better way.
> suggestions?
> 
> thanks
> 
> joe

Here is a start using a hash:

#!perl -w
my %MD = ();
my $MyData = \%MD;
my @MyWorka = ();
while ( <DATA> ){
    chomp;
    @MyWorka = split(/\s+/,$_,3);
    $MyData->{$MyWorka[1]} = $_;
 }
foreach my $MyKey (sort keys%{$MyData}) {
    printf "%-s\n", $MyData->{$MyKey};
 }
__DATA__
12/22/02 abc 123 456 789
12/23/02 def 246 812 98234
12/24/02 ank 987 23456 8762
12/27/02 abc 987 345 65434
01/05/03 abc 876 2356 87
01/09/03 ank 875 234 98098
02/01/03 def 987 3453 456
02/05/03 ghi.th 987 2345 94

#Output:

01/05/03 abc 876 2356 87
01/09/03 ank 875 234 98098
02/01/03 def 987 3453 456
02/05/03 ghi.th 987 2345 94

Wags ;)


**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to