Just push onto the correct sex which items you want from the original AoH.
use strict; use warnings; use Data::Dumper; my @data = ( { id => q{1}, name => q{Tom}, sex => q{Male}, }, { id => q{2}, name => q{Harry}, sex => q{Male}, }, { id => q{3}, name => q{Pam}, sex => q{Female}, }, { id => q{4}, name => q{Dick}, sex => q{Male}, } ); my $rhBySex = { Male => [], Female => [] }; push @{ $rhBySex->{ $_->{ sex } } }, { name => $_->{ name }, id => $_->{ id } } for @data; print Data::Dumper->Dumpxs( [ $rhBySex ], [ qw{ rhBySex } ] );
Produces
$rhBySex = { 'Female' => [ { 'id' => '3', 'name' => 'Pam' } ], 'Male' => [ { 'name' => 'Tom', 'id' => '1' }, { 'id' => '2', 'name' => 'Harry' }, { 'id' => '4', 'name' => 'Dick' } ] };
I hope this is helpful.
Cheers,
JohnGG
In reply to Re: How to filter few key value pairs from hash reference
by johngg
in thread How to filter few key value pairs from hash reference
by jaypal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |