Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

hash-array-hash confused

by mitchreward (Acolyte)
on Jul 17, 2014 at 15:14 UTC ( #1094075=perlquestion: print w/ replies, xml ) Need Help??
mitchreward has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

Am still pretty new to perl and I'd like to parse a JSON object. I'm lost between hash in arrays ...

my $json = JSON->new; my $data = $json->decode($res); my @result = $data->{'results'}; print Dumper @result;

Here is the output of data Dumper

$VAR1 = [ { 'closed_at' => 1404289092, 'elapsed_resolution_first' => '61511', 'num_messages' => '4', 'owner__context' => 'cerberusweb.contexts.worker', 'latest_message__context' => 'cerberusweb.contexts.message +', 'latest_message_id' => 4540513, 'bucket_id' => 0, } { 'closed_at' => 1404289092, 'elapsed_resolution_first' => '61511', .... } ]

So I guess this is several hash in an array, am I wrong ? And then I'd like to itterate all 'closed at' with a foreach. Any help to do this ? I've read like 4 times the perldoc on references, and Im even more confuse now.

Thanks guys

Comment on hash-array-hash confused
Select or Download Code
Replies are listed 'Best First'.
Re: hash-array-hash confused
by AppleFritter (Priest) on Jul 17, 2014 at 16:04 UTC

    Howdy mitch, welcome back to the Monastery!

    You're almost right. It's several hashes in an array indeed, but $data->{'results'} is itself an array reference, so in your example, @result is a one-element list (i.e. array) containing a single reference to an anonymous array containing several anonymous hashes.

    In order to iterate through all these, your best bet is to dereference the outermost array reference, obtaining the array of hashes again, then iterating through its elements (the hashes) and looking at each hash's closed_at key, like this:

    ... my $result_ref = $data->{'results'}; my @result = @$result_ref; foreach my $hash_ref (@result) { say "closed_at: ", $hash_ref->{'closed_at'}; ... }

    Of course, you don't have to save $result_ref and @result unless you need them. The foreach loop could also be written this way:

    ... foreach my $hash_ref (@{ $data->{'results'} }) { ... }

    The result's the same, but the above will perhaps make it a little more clear what's going on.

      thank you both, for the quality of our answers!

Re: hash-array-hash confused
by toolic (Bishop) on Jul 17, 2014 at 15:24 UTC
    Here's one way:
    use warnings; use strict; my $VAR1 = [ { 'closed_at' => 1404289092, 'elapsed_resolution_first' => '61511', 'num_messages' => '4', 'owner__context' => 'cerberusweb.contexts.worker', 'latest_message__context' => 'cerberusweb.contexts.message +', 'latest_message_id' => 4540513, 'bucket_id' => 0, }, { 'closed_at' => 1404289092, 'elapsed_resolution_first' => '61511', } ]; my @result = @{ $VAR1 }; for my $href (@result) { print "$href->{closed_at}\n"; } __END__ 1404289092 1404289092

    See also perldsc

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1094075]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2015-10-07 16:01 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (190 votes), past polls