in reply to Urgent Help: Array of Hashes
my $var = [ { "D1" => 1, "D2" => 2 }, { "D2" => 5, "D3" => 6 }, ]; my %combined; foreach my $subhash (@$var) { foreach my $key (keys %$subhash) { $combined{$key} += $subhash->{$key}; } } my @sorted_keys = sort { $combined{$b} <=> $combined{$a} } keys %combined; foreach my $key (@sorted_keys) { print("$key: $combined{$key}\n"); }
You can't actually "sort the associative array", but you can control the order in which the data is accessed. That's what @sorted_keys is about.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Urgent Help: Array of Hashes
by mirage_zz (Novice) on Sep 27, 2007 at 02:20 UTC |