perl - How can I fetch a hash ref from array of hashes by one of its values? -
perl - How can I fetch a hash ref from array of hashes by one of its values? -
i have array of hashes, each hash containing same keys values unique. on basis of particular value, need store hash ref.
see below illustration understand properly:
my @aoaoh = ( { => 1, b => 2 }, { => 3, b => 4 }, { => 101, b => 102 }, { => 103, b => 104 }, );
now check if hash key a
contains value 101
. if yes need store whole hash ref.
how that?
my $key = "a"; ($ref) = grep { $_->{$key} == 101 } @aoaoh;
or using list::util
's first()
:
use list::util 'first'; $ref = first { $_->{$key} == 101 } @aoaoh;
arrays perl hash reference perl-data-structures
Comments
Post a Comment