我想找到找到哈希值的最简单方法
City {
city1 -> Street1 -> [ high_street , 2]
street2 -> [ low_street , 2]
city2 -> Street1 -> [ high_street1 , 2]
street2 -> [ low_street2 , 2]
city3 -> Street1 -> [ high_street1 , 1]
street2 -> [ low_street2 , 1]
}
此结构被分类为哈希。如何在第二级哈希中找到数组的第二个元素的最小值?
我期待我最小的价值是 - City3,street 1 - highstreet 1.这是基于最后一次只能获得的价值。但是数组中的2个元素有1次。但我只想先了解第一个元素。
有没有容易找到它?
答案 0 :(得分:2)
my $City = {
city1 => {
Street1 => [ 'high_street', 2],
street2 => [ 'low_street', 2],
},
city2 => {
Street1 => [ 'high_street1', 2],
street2 => [ 'low_street2', 2],
},
city3 => {
Street1 => [ 'high_street1', 1],
street2 => [ 'low_street2', 1],
},
};
my $smallest_key1;
my $smallest_key2;
my $smallest_value;
foreach my $key1 (keys %{$City}) {
foreach my $key2 (keys %{$City->{$key1}}) {
if(not defined $smallest_value or $City->{$key1}{$key2}[1] < $smallest_value) {
$smallest_key1 = $key1;
$smallest_key2 = $key2;
$smallest_value = $City->{$key1}{$key2}[1];
}
}
}
print 'Smallest: ', $smallest_key1, ', ', $smallest_key2, ', ', join(', ', @{$City->{$smallest_key1}{$smallest_key2}}), "\n";