【发布时间】:2015-04-10 09:26:16
【问题描述】:
假设我有一个带有一些值的 Hash of Hash (%hash)
warn Dumper \%hash;
'Key1'=> {'Subkey1'} => {'SubSubkey11'} => {'Value1'}
'Key2'=> {'Subkey2'} => {'SubSubkey22'} => {'Value2'}
...
然后,想要验证某个键组合是否不存在(假设 if 上方有一个 for 循环遍历 $val1 和 $val2 的一系列组合)。
if (!exists $hash{$val1}{$val2} ) { #### I am only verifying the existent of the Key and Subkey, not SubSubkey
print "Doesn't exists";
}
如果我在循环结束后使用,我会看到如下内容:
warn Dumper\%hash;
'Key1' => {'Subkey1'} => {}
'Key2' => {'Subkey2'} => {}
'Key1' 和 'Subkey1' 以某种方式被“分配”给 void 值,因为我可能会在不存在的键的相同组合上循环多次,在第一次循环遍历一对键之后,第二个 if 需要组合存在。
造成这种情况的原因以及解决它的最佳方法是什么。我尝试在 if 中取消引用哈希并收到此错误
exists argument is not a HASH or ARRAY element or a subroutine
【问题讨论】: