【发布时间】:2015-11-16 22:05:38
【问题描述】:
我想动态创建一个结构如下:
{
edition1 => {
Jim => ["title1", "title2"],
John => ["title3", "title4"],
},
edition2 => {
Jim => ["titleX",],
John => ["titleY,],
} etc
}
我对如何做到这一点感到困惑。
基本上我的想法是:
my $edition = "edition1";
my $author = "Jim";
my $title = "title1";
my %main_hash = ();
${$main_hash{$edition}} ||= {};
${$main_hash{$edition}}->{$author} ||= [];
push @{{$main_hash{$edition}}->{$author}} , $title;
但不知怎的,我不确定如何正确地做到这一点,而且语法似乎很复杂。
我怎样才能以良好/清晰的方式实现我想要的?
【问题讨论】:
-
这个结构背后的目的是什么?它看起来像是一种奇怪的形状,就好像你正试图用它做其他事情,比如创建 JSON。
标签: perl hash hashtable hashref arrayref