in reply to Sorting a Hash of Hashes by Value

Short answer:

sort { $h->{$b}{s} <=> $h->{$a}{s} } keys %$h

Do you realize you have two identically-keyed hashes (%h and %$h)? Let's merge the two.

use strict; use warnings; my %totals; while (<DATA>){ next if /^$/; my ($f,$s) = split /\|/,$_; $totals{$f}{f} += $f; $totals{$f}{s} += $s; } for my $k (sort { $totals{$b}{s} <=> $totals{$a}{s} } keys %totals) { print "Key = $k\n" print "Element 1: $total{$k}{f}\n"; print "Element 3: $total{$k}{s}\n"; }

Replies are listed 'Best First'.
Re^2: Sorting a Hash of Hashes by Value
by Anonymous Monk on Oct 29, 2007 at 14:02 UTC
    I like this approach better of having all total counts merged into one single hash. Thanks for the guidance. Much appreciated.