"the" => (1, 4), "and" => (2, 3)
是一种奇怪的写作方式
"the", 1, 4, "and", 2, 3
如果分配给哈希,则相当于
%h = ();
$h{"the"} = 1;
$h{4} = "and";
$h{2} = 3;
你要
%Parent = (
1 => {
"the" => [ 1, 4 ],
"and" => [ 2, 3 ]
},
2 => {
"but" => [ 1, 2 ],
"as" => [ 3, 4 ]
}
)
就像{ }
创建一个哈希,将内部表达式的结果(如果有)分配给哈希,并返回对该哈希的引用,[ ]
创建一个数组,将内部表达式的结果(如果有)分配给数组,然后返回对该数组的引用。
当然,这些都不能回答你的问题!关于你的问题。
my %Parent = (
1 => {
"the" => [1, 4],
"and" => [2, 3]
},
2 => {
"but" => [1, 2],
"as" => [3, 4]
}
);
基本上相当于
my %Parent;
$Parent{1}{the}[0] = 1;
$Parent{1}{the}[1] = 4;
$Parent{1}{and}[0] = 2;
$Parent{1}{and}[1] = 3;
$Parent{2}{but}[0] = 1;
$Parent{2}{but}[1] = 2;
$Parent{2}{as }[0] = 3;
$Parent{2}{as }[1] = 4;
push
通常比填充数组的赋值更方便。那看起来像:
my %Parent;
push @{ $Parent{1}{the} }, 1;
push @{ $Parent{1}{the} }, 4;
push @{ $Parent{1}{and} }, 2;
push @{ $Parent{1}{and} }, 3;
push @{ $Parent{2}{but} }, 1;
push @{ $Parent{2}{but} }, 2;
push @{ $Parent{2}{as } }, 3;
push @{ $Parent{2}{as } }, 4;
多亏了自动复活,
push @{ $Parent{$k1}{$k2} }, $n;
简称
push @{ ( $Parent{$k1} //= {} )->{$k2} //= [] }, $n;