假设我有一个哈希数据结构的哈希构造如下:
%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,
},
)
我如何按number_of_members
从最大到最小的值对键(在这种情况下为家庭)进行排序?然后我想打印出最高的两个。这是一个普遍的想法,但我知道这是错误的:
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++;
};
我想要打印代码(空格是制表符分隔的):
simpsons homer;marge;bart;lisa;maggie
flintstones fred;wilma;pebbles;dino