dipamchang
dipamchang

Reputation: 77

Issue accessing Hash in perl

I have a Hash of following structure in perl -

my %testHash = (
        KeyL1 => {
            KeyLL1 => {
                KeyLLL1 => [1,2],
                KeyLLL2 => [2,3],
            },
            KeyLL2 => {
                KeyLLL1 => [1,2],
                KeyLLL2 => [2,3],
            },
            KeyLL3 => {
                KeyLLL1 => [1,2],
                KeyLLL2 => [2,3],
            },            
        },
        KeyL2 => {
            KeyLL1 => {
                KeyLLL1 => [1,2],
                KeyLLL2 => [2,3],
            },
            KeyLL2 => {
                KeyLLL1 => [1,2],
                KeyLLL2 => [2,3],
            },
            KeyLL3 => {
                KeyLLL1 => [1,2],
                KeyLLL2 => [2,3],
            }, 
        },
        );

Now, when I am trying to access it the following way, I am getting 'undef' as a result

my %tempHash = $testHash{'KeyL1'};
print Data::Dumper::Dumper($tempHash{'KeyLL1'});
print Data::Dumper::Dumper($tempHash{'KeyLL1'}{'KeyLLL1'});

Result --

$VAR1 = undef; $VAR1 = undef;

Please point to me what am I doing wrong. I am pretty new to perl.

Upvotes: 0

Views: 52

Answers (2)

User9102d82
User9102d82

Reputation: 1190

Also, you could do it this way if its just about viewing the structures.

Also try:

print Dumper $testHash{KeyL1} ;
print Dumper $testHash{KeyL1}{KeyLL1} ;
print Dumper $testHash{KeyL1}{KeyLL1}{KeyLLL1} ;

Output:

%_Host@User> ./hash.pl
$VAR1 = {
          'KeyLL1' => {
                        'KeyLLL2' => [
                                       2,
                                       3
                                     ],
                        'KeyLLL1' => [
                                       1,
                                       2
                                     ]
                      },
          'KeyLL2' => {
                        'KeyLLL2' => [
                                       2,
                                       3
                                     ],
                        'KeyLLL1' => [
                                       1,
                                       2
                                     ]
                      },
          'KeyLL3' => {
                        'KeyLLL2' => [
                                       2,
                                       3
                                     ],
                        'KeyLLL1' => [
                                       1,
                                       2
                                     ]
                      }
        };
$VAR1 = {
          'KeyLLL2' => [
                         2,
                         3
                       ],
          'KeyLLL1' => [
                         1,
                         2
                       ]
        };
$VAR1 = [
          1,
          2
        ];
%_Host@User>

Upvotes: 0

Quentin
Quentin

Reputation: 943996

The value of $testHash{'KeyL1'} is a hashref, not a hash.

Hashrefs are scalars. my %tempHash = is not expecting a scalar.

You need to dereference it:

my %tempHash = %{$testHash{'KeyL1'}};

Upvotes: 4

Related Questions