in reply to Choosing "best" - sorting hashes issue
read a file line into $line, then: my @arry = split(/==/, $line); #recent==pop==commandname $myhash{$arry[$#arry]} = [$arry[0], $arry[1]]; loop until done to sort: my @recentkeys = sort by_HoA_0_asc keys(%myhash); my @popkeys = sort by_HoA_1_desc keys(%myhash); # sort Hash of Arrays by numeric value of first # array value(history), ascending sub by_HoA_0_asc { if ($myhash{$a}[0] < $myhash{$b}[0]) { return -1; } elsif ($myhash{$a}[0] > $myhash{$b}[0]) { return 1; } return 0; } # sort Hash of Arrays by numeric value of second # array value (popularity), descending. If equal, # sort by first array value (history), ascending. sub by_HoA_1_desc { if ($myhash{$a}[1] > $myhash{$b}[1]) { return -1; } elsif ($myhash{$a}[1] < $myhash{$b}[1]) { return 1; } if ($myhash{$a}[0] < $myhash{$b}[0]) { return -1; } elsif ($myhash{$a}[0] < $myhash{$b}[0]) { return 1; } return 0; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Choosing "best" - sorting hashes issue
by dave_the_m (Monsignor) on Jan 14, 2005 at 22:18 UTC | |
by samizdat (Vicar) on Jan 17, 2005 at 14:35 UTC | |
by samizdat (Vicar) on Jan 17, 2005 at 14:57 UTC |