How to access data stored in Hash

I have this code:

$coder = JSON::XS->new->utf8->pretty->allow_nonref;
%perl = $coder->decode ($json);

When I write a variable print %perl, it says HASH (0x9e04db0). How can I access data in this hash?

+5
source share
5 answers

The return value is decodenot a hash, and you should not assign it %hash- when you do this, you destroy its value. This is a hash link and should be assigned to a scalar. Read perlreftut .

+5
source

Since the method decodeactually returns a hash reference, the correct way to assign it is:

%perl = %{ $coder->decode ($json) };

, , each .

while (my ($key, $value) = each %perl) {
    print "$key = $value\n";
}

for my $key (keys %perl) {
    print "$key = $perl{$key}\n";
} 
+13

JSON:: XS- > decode . , , :

$coder = JSON::XS->new->utf8->pretty->allow_nonref;
$perl = $coder->decode ($json);

print %{$perl};

, .

+7

, foreach loop

foreach my $key (%perl)
{
  print "$key is $perl{$key}\n";
}

a while loop

while (my ($key, $value) = each %perl)
{
  print "$key is $perl{$key}\n";
}
-3

, .

, % perl hash , 'file';

,

print $perl {'file'}; # % perl

-3

All Articles