如何在Perl中为子例程引用和取消引用哈希值

如何在Perl中为子例程引用和取消引用哈希值,perl,reference,subroutine,dereference,hash-of-hashes,Perl,Reference,Subroutine,Dereference,Hash Of Hashes,有人知道如何取消对散列的引用,以便我可以在子程序中使用它吗。如您所见,在访问子程序中的哈希数据结构时遇到问题 my $HoH_ref = \%HoH; # reference the hash of hashes for(@sorted) { print join("\t", $_, get_num($_, $HoH_ref)) } sub get_num { my ($foo) = shift; my $HoH_ref = shift; my %HoH =

有人知道如何取消对散列的引用,以便我可以在子程序中使用它吗。如您所见,在访问子程序中的哈希数据结构时遇到问题

my $HoH_ref = \%HoH;     # reference the hash of hashes

for(@sorted) {
print join("\t", $_, get_num($_, $HoH_ref))
}

sub get_num {
    my ($foo) = shift;
    my $HoH_ref = shift;
    my %HoH = %{$HoH_ref};    # dereference the hash of hashes
    my $variable = %HoH{$foo}{'name'};
    # do stuff
    return;
    }

我在倒数第二行
%HoH{$protein}{'degree'}
附近的
%HoH{
出现语法错误,哈希值无法从
%HoH
识别
$protein
键。我收到错误消息:
全局符号“$protein”需要显式的包名。谢谢,访问哈希元素的语法是
$hash{KEY}
,而不是
%hash{KEY}

my %HoH = %{$HoH_ref};
my $variable = $HoH{$foo}{name};
               ^
               |
但是复制整个散列是愚蠢的

my $variable = $HoH_ref->{$foo}{name};

访问哈希元素的语法是
$hash{KEY}
,而不是
%hash{KEY}

my %HoH = %{$HoH_ref};
my $variable = $HoH{$foo}{name};
               ^
               |
但是复制整个散列是愚蠢的

my $variable = $HoH_ref->{$foo}{name};

看不到整个脚本很难判断,但我认为你想要
$HoH{$foo}{'name'}
而不是
%HoH{$foo}{'name'}
。啊,好吧,就是这样。谢谢你看不到整个脚本很难判断,但我认为你想要
$HoH{$foo}{'name'}
而不是
%HoH{$foo}{'name'}
。啊,好的,就这样。谢谢