我将尝试用一个例子来说明这一点。举一个 Hash of Hash 的常见示例:
my %HoH = (
flintstones => {
lead => "fred",
pal => "barney",
},
jetsons => {
lead => "george",
wife => "jane",
"his boy" => "elroy",
},
simpsons => {
lead => "homer",
wife => "marge",
kid => "bart",
},
);
出于我的目的,我希望能够将未命名或匿名哈希添加到 %HOH。直到运行时,我才需要(或能够)定义这些子哈希。我怎样才能用 Perl 做到这一点?
我读过的所有内容(我已经读过 Perldocs 和 Google 的内容)似乎都显示了定义所有 sub-hahes(例如“flintstones”、“jetsons”和“simpsons”)的示例。
我正在做的是尝试构建一个父哈希,它将包含来自 CSV 文件的行的子哈希:
%TopHash = (
%Line1 => {
cell01 => $some_value1a;
cell02 => $some_value2a;
cell03 => $some_value3a;
},
%Line2 => {
cell01 => $some_value1b;
cell02 => $some_value2b;
cell03 => $some_value3b;
},
%Line3 => {
cell01 => $some_value1c;
cell02 => $some_value2c;
cell03 => $some_value3c;
},
# etc
# etc
# etc
);
直到运行时才知道我需要的“%LineX”哈希的数量(因为它们代表在运行时读取的 CSV 中的行数)。
有任何想法吗?如果还不清楚......我仍然试图围绕 Perl 散列。