How do I create a hash of hashes in Perl?

后端 未结 5 867
轮回少年
轮回少年 2021-01-18 05:26

Based on my current understanding of hashes in Perl, I would expect this code to print \"hello world.\" It instead prints nothing.

%a=();

%b=();
$b{str} = \         


        
5条回答
  •  清酒与你
    2021-01-18 06:08

    Perl likes to flatten your data structures. That's often a good thing...for example, (@options, "another option", "yet another") ends up as one list.

    If you really mean to have one structure inside another, the inner structure needs to be a reference. Like so:

    %a{1} = { %b };  
    

    The braces denote a hash, which you're filling with values from %b, and getting back as a reference rather than a straight hash.

    You could also say

    $a{1} = \%b;   
    

    but that makes changes to %b change $a{1} as well.

提交回复
热议问题