in reply to Re^2: sorting a table columns using hashes
in thread sorting a table columns using hashes
Ugly but functional#!/usr/bin/perl while (<DATA>){ chomp; @record=split(/\s+/,$_); if (defined $records{$record[0]}){ # We've seen it before and need + to compare the data if($record[1] < $records{$record[0]}->[1]){ # we have a smalle +r value and so should use this $records{$record[0]}->[1] = $record[1]; } if ( ($record[4] < $records{$record[0]}->[4]) ){ $records{$record[0]}->[3] = $record[3]; $records{$record[0]}->[4] = $record[4]; $records{$record[0]}->[6] = $record[6]; }elsif ( ( $record[4] == $records{$record[0]}->[4]) && ($recor +d[6] > $records{$record[0]}->[6]) ){ $records{$record[0]}->[3] = $record[3]; $records{$record[0]}->[6] = $record[6]; } } else{ @{$records{$record[0]}}=@record; } } for $key (reverse sort keys %records){ print join ("\t", @{$records{$key}}),"\n"; } __DATA__ xhahhxha 60 3 hahaghagah 10 1 101 xhahhxha 60 3 jrthtahtat 8 1 110 xhahhxha 60 3 shdgehsh 8 1 150 hsghtahs 100 19 hahaghagah 10 20 200 hsghtahs 100 19 jrthtahtat 10 20 300 hsghtahs 100 19 shdgehsh 10 20 400 __END__ xhahhxha 60 3 shdgehsh 8 1 150 hsghtahs 100 19 shdgehsh 10 20 400
|
|---|