Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access data stored in Hash

Tags:

json

perl

I have this code:

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

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

like image 654
Jay Gridley Avatar asked Mar 01 '10 09:03

Jay Gridley


1 Answers

As the decode method actually returns a reference to hash, the proper way to assign would be:

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

That said, to get the data from the hash, you can use the each builtin or loop over its keys and retrieve the values by subscripting.

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

for my $key (keys %perl) {
    print "$key = $perl{$key}\n";
} 
like image 173
Eugene Yarmash Avatar answered Nov 26 '22 15:11

Eugene Yarmash