如何找到与最大计算值相关的键?
我试图找出哈希中的最大值以及该最大值的对应键。我的哈希看起来像
%hash = (
bob => "4.9",
gita => "3.9 , 6.8",
diu => "3.0",
);
现在我想找到该哈希中的最大值及其所属的键。
所需的输出是
gita 6.8
我试图按升序对 %hash
中的值进行排序以获得最大值,如下所示
sub hashValueAscendingNum {
$hash{$a} cmp $hash{$b};
}
foreach my $highest (sort hashValueAscendingNum(keys(%hash))) {
print "\t $hash{$highestMagnitude} \t\t $highest \n";
}
我希望检查哈希中的所有值,并且具有最大值的值应该是带着钥匙回来了。
我怎样才能做到这一点?
I am trying to find out maximum value in a hash and corresponding key to that maximum value. My hash looks like
%hash = (
bob => "4.9",
gita => "3.9 , 6.8",
diu => "3.0",
);
Now I want to find the maximum value in that hash with the key it belongs.
Output needed is
gita 6.8
I am trying to sort the values in %hash
in ascending order to get the maximum value like this
sub hashValueAscendingNum {
$hash{$a} cmp $hash{$b};
}
foreach my $highest (sort hashValueAscendingNum(keys(%hash))) {
print "\t $hash{$highestMagnitude} \t\t $highest \n";
}
I want all the values in the hash to be checked, and the one with maximum value should be returned with its key.
How can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要首先将每个键与原始
%hash
中对应的最高值关联起来,然后找到与最高值关联的键。当然,这是非常低效的(尤其是使用
sort
),但对于您显示的尺寸,这并不重要。为了完整起见,以下是使用each
的更高效版本:You need to first associate each key with the highest of the values corresponding to it in the original
%hash
and then find the key associated with the highest value.Of course, this is very inefficient (esp. using
sort
) but for the dimensions you showed, it does not matter. For completeness, here is a more efficient version usingeach
:哈希只有一个键和一个值,并且每个键必须是唯一的。在您最初的问题中,您有这样的问题:
嗯,gita 不能有两个值。散列中也不可能有两个等于
gita
的键。因此,您不能使用简单的哈希来存储您的值。不过,有一些方法可以通过使用引用来解决这个问题。例如,散列中的每个元素都可以包含对数组的引用。因此,您的数据结构可以如下所示:
[
和]
标记对数组的引用。但是,这并不能真正解决您的特定问题,因为您现在必须遍历哈希中的每个键,然后遍历每个键的数组中的每个元素,并对它们进行排序。您可以创建一个排序子例程,但仅仅因为您可以说
sort
并不能提高它的效率。也许您需要的是数组的数组。这将解决
gita
有两个值的问题,但使排序更容易一些。想象一下这样的结构:现在,我们可以根据
$array[$x]->[1]
的值对@array
进行排序!对于数组@array
的每个元素,我们所需要做的就是将 $a->[1] 与 $b->[1] 进行比较。然后,如果我们对其进行反向排序,最大的元素将是$array[0]
。名称为$array[0]->[0]
,元素为$array->[0]->[1]
。输出是:
您注意到 Perldoc 的 perllol 的链接了吗?如果您以前从未使用过 Perl 参考资料,我建议您阅读它。
A hash has only a single key and a single value, and each key must be unique. In your original problem you have this:
Well,
gita
can't have two values. Nor, can you have two keys in your hash equal togita
. Thus, you can't use a simple hash to store your values.There are ways around this though by using references. For example, each element in your hash can contain a reference to an array. Thus, your data structure can look like this:
The
[
and]
marks a reference to an array.However, this wouldn't really solve your particular problem since you now have to go through each key in the hash, then each element in the array for each key, and sort those. You could create a sorting subroutine, but just because you can say
sort
doesn't make it efficient.Maybe what you need is an array of arrays. This will get rid of the issue you have with
gita
having two values, but make sorting a bit easier. Imagine a structure like this:Now, we can do a sort on
@array
depending upon the value of$array[$x]->[1]
! All we need is for each element of the array@array
is to compare $a->[1] with $b->[1]. Then, if we do a reverse sort on it, the biggest element will be$array[0]
. The name is$array[0]->[0]
and the element is$array->[0]->[1]
.And the output is:
You notice that link to Perldoc's perllol? I suggest you read it if you've never worked with Perl references before.
你得到了几个很好的答案。现在是一个坏的(假设你修复了哈希中的小数分隔符):
输出:
永远不要这样做,除非你故意试图打高尔夫球和/或混淆它。
You got several good answers. Now a bad one (assuming you fix the decimal separator in the hash):
Output:
Never do this unless you're deliberately trying to golf and/or obfuscate it though.