Reputation: 1030
Given the following anonymous array of hashes:
$AoH = [
{
'FORM_FIELD_ID' => '10353',
'VISIBLE_BY' => '10354',
'FIELD_LABEL' => 'ISINCIDENT',
'VALUE' => '',
'DEFAULT_FIELD_LABEL' => 'Yes No',
'FORM_ID' => '2113',
},
{
'FORM_FIELD_ID' => '10354',
'VISIBLE_BY' => '0',
'FIELD_LABEL' => 'CATEGORY',
'VALUE' => 'zOS Logical Security (RACF)',
'DEFAULT_FIELD_LABEL' => 'CATEGORY',
'FORM_ID' => '2113',
},
{
'FORM_FIELD_ID' => '10368',
'VISIBLE_BY' => '10354',
'FIELD_LABEL' => 'STARTDATE',
'VALUE' => '',
'DEFAULT_FIELD_LABEL' => 'REQTYPE',
'FORM_ID' => '2113',
}
];
How would I directly access the FIELD_LABEL
value given that I knew the FORM_FIELD_ID
is 10353?
I know I can loop through @$AoH
and conditionally find $_->{FIELD_LABEL}
based on $_->{FORM_FIELD_ID} == 10353
, but is there anyway to directly access the wanted value if one of the other values in the same hash is known?
Upvotes: 2
Views: 109
Reputation: 6378
You'd have to write a function loop through @array
and examine the %hash
or maybe use the builtin grep
method:
say $_->{FIELD_LABEL} for (grep { $_->{FORM_FIELD_ID} == 10353 } @$AoH )
works. And so does this:
say %$_->{FIELD_LABEL} for (grep { $_->{FORM_FIELD_ID} == 10353 } @$AoH )
but it gives a Using a hash as a reference is deprecated
warning (with pumpkin perl-5.16.3
).
Upvotes: 0
Reputation: 57600
No, not unless you change your data structure. You could e.g. index the records by their form field id:
my %by_form_field_id = map { $_->{FORM_FIELD_ID} => $_ } @$AoH;
Then:
my $field_label = $by_form_field_id{10353}{FIELD_LABEL};
Without changing the data structure, you really have to grep
:
my $field_label = (grep { $_->{FORM_FIELD_ID} == 10353 } @$AoH)[0]->{FIELD_LABEL};
Upvotes: 4