我有一個子程序,返回一個散列的引用。我想從該散列獲得第一個鍵,並在子程序返回後爲其分配一個變量。現在,我這樣做的,這工作,但我覺得應該有一個更聰明/更簡單的方法從哈希分配密鑰到變量
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'模塊。 –
正確。沒有「第一把鑰匙」。 –