如何构建哈希的哈希
我需要比较两个哈希值,但我无法获取内部密钥集......
my %HASH = ('first'=>{'A'=>50, 'B'=>40, 'C'=>30},
'second'=>{'A'=>-30, 'B'=>-15, 'C'=>9});
foreach my $key (keys(%HASH))
{
my %innerhash = $options{$key};
foreach my $inner (keys(%innerhash))
{
print "Match: ".$otherhash{$key}->{$inner}." ".$HASH{$key}->{$inner};
}
}
I need to compare two hashes, but I can't get the inner set of keys...
my %HASH = ('first'=>{'A'=>50, 'B'=>40, 'C'=>30},
'second'=>{'A'=>-30, 'B'=>-15, 'C'=>9});
foreach my $key (keys(%HASH))
{
my %innerhash = $options{$key};
foreach my $inner (keys(%innerhash))
{
print "Match: ".$otherhash{$key}->{$inner}." ".$HASH{$key}->{$inner};
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$options{$key}
是一个标量(您可以看出它是领先的$
符号)。您想要“取消引用”它以将其用作散列:当您准备好真正深入研究这些内容时,请参阅
perllol
、perldsc
和<一个href="http://search.cpan.org/perldoc?perlref" rel="nofollow">perlref
。$options{$key}
is a scalar (you can tell be the leading$
sigil). You want to "dereference" it to use it as a hash:When you're ready to really dive into these things, see
perllol
,perldsc
andperlref
.我猜你说的是“选项”,而你的意思是“哈希”?
哈希值只存储标量,不存储其他哈希值; %HASH 的每个值都是需要取消引用的哈希引用,因此您的内部循环应该是:
或者:
I'm guessing you say "options" there where you mean "HASH"?
Hashes only store scalars, not other hashes; each value of %HASH is a hash reference that needs to be dereferenced, so your inner loop should be:
Or: