in reply to accessing data in hash

Hi ccfc1986,

..I want to be able to loop through the data, and check the current JobID, when i find a match (say 33) i want to grab the State field and do a check against it..

You mean something like so:

print map { $_->{State} if $_->{ID} == 32 } @{ $hash->{data} };
OR
for my $state ( @{ $hash->{data} } ) { print $state->{State} if $state->{ID} == 32; }

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^2: accessing data in hash
by AnomalousMonk (Archbishop) on Mar 29, 2014 at 20:38 UTC
    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' '' ''

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

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

      -- Ken