这不是我填充哈希的方式。为了更容易阅读,这里是它的内容,键在一个固定长度的字符串上:
my %country_hash = (
"001 Sample Name New Zealand" => "NEW ZEALAND",
"002 Samp2 Nam2 Zimbabwe " => "ZIMBABWE",
"003 SSS NNN Australia " => "AUSTRALIA",
"004 John Sample Philippines" => "PHILIPPINES,
);
Run Code Online (Sandbox Code Playgroud)
我想根据值获取排序的键。所以我的期望:
"003 SSS NNN Australia "
"001 Sample Name New Zealand"
"004 John Sample Philippines"
"002 Samp2 Nam2 Zimbabwe "
Run Code Online (Sandbox Code Playgroud)
我做了什么:
foreach my $line( sort {$country_hash{$a} <=> $country_hash{$b} or $a cmp $b} keys %country_hash ){
print "$line\n";
}
Run Code Online (Sandbox Code Playgroud)
还; (我怀疑这会排序,但无论如何)
my @sorted = sort { $country_hash{$a} …Run Code Online (Sandbox Code Playgroud)