我將線索的哈希引用存儲到共享的@stories變量,然後無法訪問它們。無法從hashref獲取哈希值
my @stories : shared=();
sub blah {
my %stories : shared=();
<some code>
if ($type=~/comment/) {
$stories{"$id"}="$text";
$stories{"$id"}{type}="$type";
lock @stories;
push @stories, \%stories;
}
}
# @stories is a list of hash references which are shared from the threads;
foreach my $story (@stories) {
my %st=%{$story};
print keys %st; # <- printed "8462529653954"
print Dumper %st; # <- OK
my $st_id = keys %st;
print $st_id; # <- printed "1"
print $st{$st_id}; # <- printed "1/8"
}
print keys %st works as expected but when i set in to a variable and print, it returns "1".
Could you please advice what I'm doing wrong. Thanks in advance.
你期望發生的? '$ st_id = keys%st'與'$ st_id = scalar(keys%st)'相同,意思是將'$ st_id'設置爲散列'%st'中的鍵數。 – mob
我希望$ st_id將是一個關鍵。即8462529653954. –
您是否知道「my%st =%{$ story}」正在製作哈希的副本?對'%st'所做的任何更改都不會反映在原始哈希引用中。 – cjm