How do I create a hash of hashes in Perl?

后端 未结 5 871
轮回少年
轮回少年 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:06

    1. You should always use "use strict;" in your program.

    2. Use references and anonymous hashes.

    use strict;use warnings;
    my %a;
    
    my %b;
    $b{str} = "hello";  
    $a{1}={%b};
    
    %b=();
    $b{str} = "world";
    $a{2}={%b};
    
    print "$a{1}{str}  $a{2}{str}";
    

    {%b} creates reference to copy of hash %b. You need copy here because you empty it later.

提交回复
热议问题