【问题标题】:how to increment hash of hash in perl如何在perl中增加散列的散列
【发布时间】:2015-07-17 06:42:20
【问题描述】:

未能使用以下代码正确填充 HoH: 当我使用以下运行循环时:

while (my $form = $form_rs->next ()){
    my $menu=$form->get_column("fmenu");
    my $script=$form->get_column("fscript");
    my $name=$form->get_column("ftitle");

    $itemList->{$menu} = {
        $script => $name
    };
}
print Dumper $itemList;

它运行正确,但由于 $menu 重复,它只保留 HoH 中的最后一个值。所以我在 Data Dumper 中得到错误的输出。每个“菜单”我只得到 1 条记录,而应该有很多。

得到:

itemList=>{
    menu1=>{
        script1=>formName1
    },
    menu2=>{
        script3=>formName3
    }
    ...(and so on)
}

而预期:

itemList=>{
    menu1=>{
        script1=>formName1,
        script2=>formName2
    },
    menu2=>{
        script3=>formName3,
        ...(and so on)
    }
    ...(and so on)
}

请帮忙。 谢谢。

【问题讨论】:

    标签: perl hash hash-of-hashes


    【解决方案1】:

    那么您想更新$itemList->{$menu}{$script},而不是将一个元素哈希的引用分配给$itemList->{$menu}

    $itemList->{$menu}{$script} = $name;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-10
    • 2013-07-19
    • 1970-01-01
    • 2011-03-13
    • 2016-07-18
    • 2010-12-31
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多