in reply to Re: accessing data in hash
in thread accessing data in hash

print map { $_->{State} if $_->{ID} == 32 } @{ $hash->{data} };

ccfc1986: Be aware that the processing of this data flow is IMHO problematic. For every element processed that does not match the desired ID, an extraneous empty string is returned (the  '' pairs in the example below), the result of the false evaluation | evaluation as false of the  if conditional expression. I think NetWallah's use of grep is preferable.

c:\@Work\Perl\monks>perl -wMstrict -le "my $hash = { 'data' => [ { 'ID' => 32, 'State' => 'Stopped' }, { 'ID' => 33, 'State' => 'Stopped' }, { 'ID' => 31, 'State' => 'Running' }, ], }; ;; my @ra = map { $_->{State} if $_->{ID} == 32 } @{ $hash->{data} }; printf qq{'$_' } for @ra; " 'Stopped' '' ''

Replies are listed 'Best First'.
Re^3: accessing data in hash
by kcott (Archbishop) on Mar 29, 2014 at 21:43 UTC

    You make a good point. Writing the map like this removes those empty strings from the result:

    map { $_->{ID} == 32 ? $_->{State} : () }

    -- Ken