in reply to Proper Way to Reference a Hash Value

Hi, is your data originally JSON? If so I would skip turning it into a Perl hash and use a tool for JSON like jq.

On the other hand if you have already got an array of hashrefs like that, I would cut them down to size with map. Something like:

my $families = [{ name => "Flintstone", members => { Dad => { name => +"Fred", pet => "Dino" }, Mom => { name => "Wilma" } } }, { name => "R +ubble", members => { Dad => { name => "Barney", pet => "Hoppy" }, Mom + => { name => "Betty" } } }]; # deref the arrayref $families, then transform the list of hashrefs in +to a new list of # hashrefs containing just one k=v pair, extracted from the input stru +cture my @family_pets = map { {$_->{name} => $_->{members}{Dad}{pet}} } @{ $ +families }; say Dumper \@family_pets;
Output
$VAR1 = [ { 'Flintstone' => 'Dino' }, { 'Rubble' => 'Hoppy' } ];

Hope this helps!


The way forward always starts with a minimal test.