contravaluebets
contravaluebets

Reputation: 85

Derefence hash of hash variable

How do I dereference such a variable to get '99' and 'Problem in Parameter' ?

  DB<103> print Dumper($error_code);
  $VAR1 = \{
            '99' => 'Problem in Parameter'
          };


  DB<104> x $error_code
  0  REF(0x30393f34)
   -> HASH(0x30393ea4)
         99 => 'Problem in Parameter'

Upvotes: 1

Views: 70

Answers (3)

choroba
choroba

Reputation: 242208

You have a reference to a reference. You have to double dereference, first as scalar, than as hash:

my $error_code = \{
  '99' => 'Problem in Parameter'
};
my ($ninety_nine) = keys %$$error_code;
my $string        = ${$error_code}->{$ninety_nine};
print "$ninety_nine, $string\n";

Upvotes: 1

ikegami
ikegami

Reputation: 386561

You have a reference to a reference to a hash. So you need two dereferences.

$error_code    A reference to a reference to a hash
$$error_code   A reference to a hash
%$$error_code  A hash

So,

my ($key, $value) = %$$error_code;
say $key;
say $value;

Upvotes: 1

Toto
Toto

Reputation: 91528

How about:

my ($key, $value) = each %$$error_code;
say $key;
say $value;

output:

99
Problem in Parameter

Upvotes: 0

Related Questions