Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I want to sort by $h->{$f}{'s'} so that the total of the third element is sorted as: 8 8 3 2 rather than by element two: 10 8 4 1. How can I achieve this? Thanks.use Data::Dumper; while (<DATA>){ next if /^$/; ($f,$s) = split /\|/,$_; $h{$f} += $f; $h->{$f}{'s'} += $s; } for my $k (sort {$h{$b} <=> $h{$a} } keys %h){ print "Key = $k\nElement 1: $h{$k}\nElement 3: $h->{$k}{'s'}\n"; }; #print Dumper{%{$h}}; __DATA__ 1|2 2|4 2|4 4|3 4|5 5|1 5|2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting a Hash of Hashes by Value
by ikegami (Patriarch) on Oct 29, 2007 at 13:46 UTC | |
by Anonymous Monk on Oct 29, 2007 at 14:02 UTC | |
|
Re: Sorting a Hash of Hashes by Value
by Anonymous Monk on Oct 29, 2007 at 13:45 UTC |