perlperl-hash

dereferencing nested hash perl


If I declare a hash like this:

my %n = (a => {1 => "1a",2 => "2a"},"b" => {1 => "1b",2 => "2b"});

How do I access the data in: n -> a -> 1?

I tried

print "$n{a{1}}";

print "$n{a ->{ 1}}";

These two don't work


Solution

  • Refer to perldsc

    use warnings;
    use strict;
    
    my %n = (a => {1 => "1a",2 => "2a"},"b" => {1 => "1b",2 => "2b"});
    print "$n{a}{1}\n";
    
    __END__
    
    1a