--- Joseph Paish <[EMAIL PROTECTED]> 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?
Don't keep the whole array. Just save each as your read it into a holding scalar, and print it when the 2nd key isn't the same anymore. Assuming you've already sorted by the second field: my $prevrec = <>; my $key = substr($prevrec,9,3); while(<>) { my $tmp = substr($_,9,3); next if $key eq $tmp; print $prevrec; $key = $tmp; $prevrec = $_; } print $prevrec; # either last of same, or only of new! Somebody double-check me on this? __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - forms, calculators, tips, more http://taxes.yahoo.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]