%HoH
is declared as a hash, but is defined as a hashreference. Use parentheses(...)
instead of braces{...}
.- You don't need to loop through the hash to sort it. Sort will take care of that.
- if you
sort {...} keys %HoH
, then the special variables$a
and$b
represent the keys of%HoH
as it performs the sort. $a
and$b
are in reverse order because your expected result is in decreasing order. (Update: Oh I just noticed that you had that in the first place.)The
zip
value in the nested hash is$HoH{$KEY}{'zip'}
, which is what you should sort by.use strict; use warnings; use Data::Dumper; my %HoH = ( 'foo1' => { 'bam' => 1, 'zip' => 0, }, 'foo2' => { 'bam' => 0, 'zip' => 1, 'boo' => 1 } ); my @sorted = sort {$HoH{$b}{'zip'} <=> $HoH{$a}{'zip'}} keys %HoH; print Dumper \@sorted;
Note that the result of this code will give you an array:
$VAR1 = [
'foo2',
'foo1'
];
... not a nested array:
$VAR1 = [
['foo2', 'foo1']
];