我在这里看到了类似的问题和恶心,但没有一个能够专门回答我自己的问题.
我试图以编程方式创建哈希哈希.我的问题代码如下:
my %this_hash = ();
if ($user_hash{$uuid})
{
%this_hash = $user_hash{$uuid};
}
$this_hash{$action} = 1;
$user_hash{$uuid} = %this_hash;
my %test_hash = $user_hash{$uuid};
my $hello_dumper = Dumper \%this_hash;
Run Code Online (Sandbox Code Playgroud)
根据我的输出,$ this_hash被正确分配但是
$user_hash{$uuid} = %this_hash
Run Code Online (Sandbox Code Playgroud)
在调试器中显示值为1/8; 不确定他的意思.我也收到一个警告:"哈希分配中奇数个元素......"
hob*_*bbs 12
你写的任何时候
%anything = $anything
Run Code Online (Sandbox Code Playgroud)
你做错了什么.几乎在你写的任何时候
$anything = %anything
Run Code Online (Sandbox Code Playgroud)
你做错了什么.这包括何时$anything是数组或散列访问(即$array[$index]或$hash{$key}).存储在数组和散列中的值始终是标量,而数组和散列本身不是标量.因此,当您想要在哈希中存储哈希时,您存储对它的引用:$hash{$key} = \%another_hash.而当你要访问年代曾对它的引用存储在哈希散列,取消引用:%another_hash = %{ $hash{$key} }或$hashref = $hash{$key}; $value = $hashref->{ $another_key }或$value = $hash{$key}{$another_key}.
为了快速了解参考文献,我强烈建议您阅读Perl References Tutorial和Perl Data Structures Cookbook.
这不是真正的"散列哈希"; 它是"哈希引用的哈希".
尝试:
$user_hash{$uuid} = \%this_hash;
Run Code Online (Sandbox Code Playgroud)