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 > > 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? >
if the file is already sorted, using a hash will do the trick: #!/usr/bin/perl -w use strict; my %hash; open(DATA,"sorted_file.txt") || die $!; $hash{(split)[1]} = $_ while(<DATA>); close(DATA); print for(values %hash); __END__ prints: 02/05/03 ghi.th 987 2345 94 02/01/03 def 987 3453 456 01/05/03 abc 876 2356 87 01/09/03 ank 875 234 98098 the disadvantage is that the output is bit out of order. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]