我有一个子程序,返回一个散列的引用。我想从该散列获得第一个键,并在子程序返回后为其分配一个变量。现在,我这样做的,这工作,但我觉得应该有一个更聪明/更简单的方法从哈希分配密钥到变量
my $hash = get_value(); # calling my subroutine here which returns a reference to a hash
foreach my $keys (keys %{$hash}) {
my $test_variable = $keys; # Assigning the first key to variable
# check if variable is not empty and exit the loop
if (!$test_variable) {
last;
}
}
**哈希不存储元素的顺序**如果你需要订单,你使用一个数组。如果你真的需要一个有序的哈希,看看'Tie :: IxHash'模块。 –
正确。没有“第一把钥匙”。 –