in reply to How to filter few key value pairs from hash reference

Have you tried printing Dumper \@data after you populate $data2? It seems you aren't creating a copy of the hash. To do that, you'd need something like
for my $href ( @data ) { my $copy_ref = { %$href }; delete $copy_ref->{sex}; push @{ $data2->{$href->{sex}} }, $copy_ref; }

Or, you can construct the target hash on the fly:

for my $href (@data) { push @{ $data3->{ $href->{sex} } }, { map 'sex' eq $_ ? () : ( $_ => $href->{$_} ), keys %$href } +; }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: How to filter few key value pairs from hash reference
by jaypal (Beadle) on Sep 15, 2014 at 17:23 UTC

    Hi Choroba,

    Yes, I typed that up in a hurry. I just updated to reflect the use of dclone to copy a hash reference.

    Thanks for the alternatives, I will try them out shortly.