我无法弄清楚如何创建几个%th2
结构(见下文),每个结构都是 , 等的$th1{0}
值$th1{1}
。
我还试图弄清楚如何遍历第二个 hash 中的键%th2
。我遇到了在 SO 中经常讨论的那个错误,
Can't use string ("1") as a HASH ref while "strict refs" in use
此外,当我分配%th2
给 中的每个键时%th1
,我假设这被复制%th1
为匿名哈希,并且我在重用时不会覆盖这些值%th2
。
use strict;
my %th1 = ();
my %th2 = ();
my $idx = 0;
$th2{"suffix"} = "A";
$th2{"status"} = 0;
$th2{"consumption"} = 42;
$th1{$idx} = %th2;
$idx++;
$th2{"suffix"} = "B";
$th2{"status"} = 0;
$th2{"consumption"} = 105;
$th1{$idx} = \%th2;
for my $key1 (keys %th1)
{
print $key1."\n\n";
for my $key2 (keys %$key1)
{
print $key2->{"status"};
}
#performing another for my $key2 won't work. I get the strict ref error.
}