jaySingh
jaySingh

Reputation: 13

How to iterate through a hash of hashes which contains an array of hashes in Perl?

I am getting an output to a query in the format below. I need to get all the values for itemthree.

After trying a lot of different things I was able to extract only the first value. I couldn't figure out how to run it in a loop so as to get the values of all itemthree in the items element.

This is a dump of my data

$VAR1 = {

    one => { msgSize => 103 },

    two => {
        items => [
            { itemOne => -1, itemthree => "AB_CD_EF", itemtwo => 0 },
            { itemOne => -1, itemthree => "XY_YZ_AB", itemtwo => 10 },
        ],
    },

    someOtherStuff => "abc",
}

Upvotes: 1

Views: 69

Answers (1)

stevieb
stevieb

Reputation: 9296

Assuming you have a hash:

my @item3s;

for my $item (@{ $hash{two}{items} }){
    push @item3s, $item->{itemthree};
}

print "$_\n" for @item3s;

If it's in fact a hash reference, change $hash{two}{items} to $hash->{two}{items}

Upvotes: 1

Related Questions