gpt4 book ai didi

Perl %{$var} 与 %$var

转载 作者:行者123 更新时间:2023-12-02 19:38:45 24 4
gpt4 key购买 nike

%{$var}%$var 有什么区别?我尝试了这段代码,但出现错误:

each on reference is experimental at test.pl line 21. Type of argument to each on reference must be unblessed hashref or arrayref at test.pl line 21.

use feature 'say';

%HoH = (
1 => {
husband => "fred",
pal => "barney",
},
2 => {
husband => "george",
wife => "jane",
"his boy" => "elroy",
},
3 => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);

for ($i = 1; $i <= 3; $i++) {
while ( ($family, $roles) = each %$HoH{$i} ) {
say "$family: $roles";
}
}

但是这段代码工作正常:

use feature 'say';

%HoH = (
1 => {
husband => "fred",
pal => "barney",
},
2 => {
husband => "george",
wife => "jane",
"his boy" => "elroy",
},
3 => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);

for ($i = 1; $i <= 3; $i++) {
while ( ($family, $roles) = each %{$HoH{$i}} ) {
say "$family: $roles";
}
}

最佳答案

使用 %$HoH{$i} 可以创建 $HoH 的哈希引用,而使用 %{$HoH{$i}} 则可以创建哈希引用$HoH{$i},这就是您想要的...并且,在您的代码上使用 strict :-)

关于Perl %{$var} 与 %$var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com