简体   繁体   English

在Perl中,如何在哈希中打印与最大值相对应的键?

[英]In Perl, how can I print the key corresponding to the maximum value in a hash?

How can I print only the first key and element of my hash? 如何仅打印哈希的第一个键和元素?

I have already a sorted hash, but I want to print only the first key and respective value thanks, 我已经有一个经过排序的哈希,但是我只想打印第一个键和相应的值,谢谢,

Thanks to all of you at the end I push the keys and the values to two different @array and print element 0 of each array and it works :) 最后,感谢大家,我将键和值推到两个不同的@array并打印每个数组的元素0,它的工作原理是:)

Hashes have unordered keys. 哈希具有无序的密钥。 So, there is no such key as a first key in a hash. 因此,哈希中没有第一个密钥之类的密钥。

However, if you need the key that sorts first (for maximum key value): 但是,如果您需要首先排序的键(用于最大键值):

my %hash = (
    'foo' => 'bar',
    'qux' => 'baz',
);

my ($key) = sort { $b cmp $a } keys %hash;
print "$key => $hash{$key}";  # Outputs: qux => baz

Remember to use <=> instead of cmp for numerical sorting. 切记使用<=>代替cmp进行数字排序。

In perl hashes there is no ordering for keys. 在perl哈希中,没有密钥顺序。 Use sort function to get the keys in the order that you want or you can push the keys into an array as you create the hash and your first key will be in zero th index in the array 使用排序功能按所需顺序获取键,或者在创建哈希时将键推入数组,并且第一个键将位于数组的第零个索引中

You can use the below code, i am assuming hash name is my_hash and keys and values are numbers. 您可以使用以下代码,我假设哈希名称为my_hash,键和值为数字。 If you have strings, you can use cmp instead of <=> . 如果您有字符串,则可以使用cmp代替<=> Refer to the sort documentation for more details 请参阅排序文档以获取更多详细信息

Get the max key 获取最大密钥

foreach (sort {$b <=> $a} keys %my_hash) {
    print "Keys is $_\n";
    print "Value is $my_hash{$_}\n";
    last;
}

Get the key corresponding to the max value 获取对应于最大值的密钥

foreach (sort {$my_hash{$b} <=> $my_hash{$a}} keys %my_hash) {
    print "Keys is $_\n";
    print "Value is $my_hash{$_}\n";
    last;
}

Just as Alan wrote - hashes don't have specific order, but you can sort hash keys: 正如Alan所写-哈希没有特定的顺序,但是您可以对哈希键进行排序:

foreach my $key (sort keys(%hash)) {
   print $key . ': ' . $hash{$key} . "\n";
}

or, as you wish, get first element from keys array: 或者,根据需要,从keys数组中获取第一个元素:

my @keys = keys(%hash);
print $keys[0];
foreach my $key (sort keys(%hash)) { 
   print "$key" .  "$hash{$key}" . "\n"; 
   last;
} 

For large hashes, if you do not need the sorted keys for any other reason, it might be better to avoid sorting. 对于较大的哈希,如果由于任何其他原因不需要排序的键,最好避免排序。

#!/usr/bin/env perl

use strict; use warnings;

my %hash = map { $_ => rand(10_000) } 'aa' .. 'zz';

my ($argmax, $max) = each %hash;
keys %hash; # reset iterator

while (my ($k, $v) = each %hash) {
    if ($v >= $max) {
        $max = $v;
        $argmax = $k;
    }
}

print "$argmax => $max\n";

If you are intent on sorting, you only need the key with the maximum value, not the entire arrays of keys and values : 如果您打算进行排序,则只需要具有最大值的键,而不需要keysvalues的整个数组:

#!/usr/bin/env perl

use strict; use warnings;

my %hash = map { $_ => rand(10_000) } 'aa' .. 'zz';
my ($argmax) = sort { $hash{$b} <=> $hash{$a} } keys %hash;

print "$argmax => $hash{$argmax}\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM