Perl:哈希 - 按值排序并获取密钥

chr*_*s01 0 perl hash

是否有一些perl"魔法"迭代通过值排序的哈希值,但在迭代中获取密钥?

按X排序并获得X很容易(键,值) - 以及按键排序并获取值.

谢谢!

hau*_*kex 5

my %hash = ( one => 'a', two => 'b', three => 'c', four => 'd' );
for my $k ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {
    print "$k: $hash{$k}\n";
}
__END__
one: a
two: b
three: c
four: d
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

84 次

最近记录:

7 年,8 月 前