Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to filter few key value pairs from hash reference

by jaypal (Beadle)
on Sep 15, 2014 at 16:55 UTC ( [id://1100620]=perlquestion: print w/replies, xml ) Need Help??

jaypal has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks,

I need some suggestions in filtering out few key value pairs from a hash reference. Here is a quick example that will probably demonstrate what I am trying to do (you will notice that in my final data structure the array of hashes has one key value pair less as it has been moved outside of array):

Lets assume I have a following data structure:

@data = ( { id => '1', name => 'Tom', sex => 'Male', }, { id => '2', name => 'Harry', sex => 'Male', }, { id => '3', name => 'Pam', sex => 'Female', }, { id => '4', name => 'Dick', sex => 'Male', } );

From this array of hashes, I am interested in creating a hash of array of hashes which will have inner key of sex whose value will be array of hashes.

My attempt to do this was as follows:

use strict; use warnings; use Storable qw / dclone /; #my @data = ( ... ) As shown above; my $data2; for my $href ( @data ) { my $copy_ref = dclone($href); my $sex = $copy_ref->{sex}; delete $copy_ref->{sex}; push @{ $data2->{$sex} }, $copy_ref; } use Data::Dumper; print Dumper $data2;

This was the output:

$VAR1 = { 'Female' => [ { 'name' => 'Pam', 'id' => '3' } ], 'Male' => [ { 'name' => 'Tom', 'id' => '1' }, { 'name' => 'Harry', 'id' => '2' }, { 'name' => 'Dick', 'id' => '4' } ] };

I am creating a copy of the hash reference and then manipulating the copy reference and then adding it to new data structure to protect my original data structure. Is there a better way to filter certain key value pairs from a hash reference without having to create a copy reference but protecting the original data structure.

Since this is only for learning purposes, I am interested in looking at other options even if they come at the cost of readability.

Looking forward to your wisdom.

Regards
Jaypal

Replies are listed 'Best First'.
Re: How to filter few key value pairs from hash reference
by choroba (Cardinal) on Sep 15, 2014 at 17:20 UTC
    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 } +; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.

Re: How to filter few key value pairs from hash reference
by johngg (Canon) on Sep 15, 2014 at 17:23 UTC

    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

      Thanks JohnGG, appreciate the alternative.
Re: How to filter few key value pairs from hash reference
by CountZero (Bishop) on Sep 15, 2014 at 20:28 UTC
    I assume the data in your array of hashes will be sourced from an external file and will be much larger than a few records. Why then not transform that external file into a "real" database which you can manipulate with standard SQL queries? In the long run that will save time and effort.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Thanks CountZero for the suggestion. This was just a learning exercise where I was looking for copying a subset of a hash. Typically, I would use a hash slice but here I was looking to copy the key value pairs.

      The map solution posted by choroba seems to be exactly what I needed (without creating a temporary copy of hash reference) though impairs readability (probably due to ternary op). Solution by JohnGG looks promising and quite readable but may not scale well if we have large sets of key value pairs to keep.

      Still hoping to find a sweet spot between the two solutions.

        Typically, I would use a hash slice
        Time to upgrade to 5.20, it seems: New slice syntax.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to filter few key value pairs from hash reference
by einhverfr (Friar) on Sep 16, 2014 at 05:43 UTC

    A simple way to do this would be to copy using map and grep. Note since you are doing this multiple times you will probably be better off with a for loop in this case, but this is good to know because if you don't want to do this multiple times, map is usually cleaner:

    my $categorized_data = { Male => map { id => $_->{id}, name => $_->{name} } grep { $_->{sex} eq 'Male' } @data, Female => map { id => $_->{id}, name => $_->{name} } grep { $_->{sex} eq 'Female' } @data, };

    Naturally the above is just an example. You probably wouldn't want to grep and map each list multiple times if it was very large, but if you were filtering a small number of keys on a small subset of a list that's how I would do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100620]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found