Let's say I have a hash of hashes data structure constructed as followed:
%HoH => (
flintstones => {
family_members => "fred;wilma;pebbles;dino",
number_of_members => 4,
},
jetsons => {
family_members => "george;jane;elroy",
number_of_members => 3,
},
simpsons => {
family_members => "homer;marge;bart;lisa;maggie",
number_of_members => 5,
},
)
How do I sort the keys, the families in this case, by the value number_of_members
from greatest to least? Then I would like to print out the highest two. Here's a general idea but I know it's wrong:
foreach $value (
sort {
$HoH{$a}{$number_of_members} cmp $HoH{$b}{$number_of_members}
} keys %HoH)
my $count = 0;
while ($key, $value) = each %HoH) {
if (count <= 2){
print "${HoH}{$key}\t$key{$value}";
}
}
continue {
$count++;
};
I want the code to print (the spaces are tab delimited):
simpsons homer;marge;bart;lisa;maggie
flintstones fred;wilma;pebbles;dino