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

发布于 2024-12-16 11:30:21 字数 120 浏览 0 评论 0原文

如何仅打印哈希的第一个键和元素?

我已经有一个排序的哈希,但我只想打印第一个键和相应的值 谢谢,

感谢大家,最后我将键和值推送到两个不同的 @array 并打印每个数组的元素 0 并且它有效:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妞丶爷亲个 2024-12-23 11:30:21

哈希值具有无序的键。因此,哈希中不存在第一个键这样的键。

但是,如果您需要首先排序的键(以获得最大键值):

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

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

请记住使用 <=> 而不是 cmp 进行数字排序。

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.

有木有妳兜一样 2024-12-23 11:30:21

在 Perl 哈希中,键没有排序。使用 sort 函数按您想要的顺序获取键,或者您可以按键创建哈希时将其放入数组中,并且您的第一个键将位于数组中的第零个索引中

您可以使用下面的代码,我假设哈希名称是 my_hash,键和值是数字。如果您有字符串,则可以使用 cmp 而不是 <=>。更多详情请参阅排序文档

获取最大值键

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

获取最大值对应的键

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

In perl hashes there is no ordering for keys. 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. If you have strings, you can use cmp instead of <=>. 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;
}
雪花飘飘的天空 2024-12-23 11:30:21
foreach my $key (sort keys(%hash)) { 
   print "$key" .  "$hash{$key}" . "\n"; 
   last;
} 
foreach my $key (sort keys(%hash)) { 
   print "$key" .  "$hash{$key}" . "\n"; 
   last;
} 
我的奇迹 2024-12-23 11:30:21

对于大型哈希,如果您出于任何其他原因不需要排序的键,那么最好避免排序。

#!/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";

如果您打算排序,则只需要具有最大值的键,而不是 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";

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:

#!/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";
静赏你的温柔 2024-12-23 11:30:21

正如艾伦所写 - 散列没有特定的顺序,但您可以对散列键进行排序:

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

或者,如您所愿,从键数组中获取第一个元素:

my @keys = keys(%hash);
print $keys[0];

Just as Alan wrote - hashes don't have specific order, but you can sort hash keys:

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

or, as you wish, get first element from keys array:

my @keys = keys(%hash);
print $keys[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文