What is the easiest way to get a key with the high

2020-02-08 05:59发布

What is the easiest way to get a key with the highest value from a hash in Perl?

标签: perl hash max
8条回答
▲ chillily
2楼-- · 2020-02-08 06:44
my $highest_val = (sort { $hash{$a} <=> $hash{$b} } keys %hash)[0];

is likely to be what you want.

If you have a very large hash, you might want to use something like a Schwartzian transform:

my @array = map {[$hash{$_},$_]} keys %hash;
my $key_with_highest_value = (sort { $a->[0] <=> $b->[0] } @array)[0]->[1]
查看更多
贪生不怕死
3楼-- · 2020-02-08 06:51
my $highest_val = (keys {$hash{$b} <=> $hash{$a}} keys %hash)[0];
查看更多
登录 后发表回答