Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Filter array of hashes for specific keys

by ddominnik (Novice)
on Oct 21, 2016 at 09:45 UTC ( [id://1174434]=note: print w/replies, xml ) Need Help??


in reply to Re: Filter array of hashes for specific keys
in thread Filter array of hashes for specific keys

So far I've tried it myself this way:

sub filter { die 'Error: no fields to filter through' if (@_ < 2); my $response = shift; my @fields; foreach my $a (0..$#_) { $fields[$a]= shift; } if (ref $response eq 'ARRAY') { my @response = @$response; my @filtered_responsearr; my %filtered_response; foreach my $ref (@response) { my %response = %$ref; foreach my $field (@fields) { $filtered_response{$field}=$response{$field}; } push @filtered_responsearr, %filtered_response; } return \@filtered_responsearr; }

which returns an array of all the fields, like ['name','foo','id','bar','name','foo2','id','bar2']
But I don't need an array of all the fields, I need an array of hashes, because I have to work with the hashes afterwards

Replies are listed 'Best First'.
Re^3: Filter array of hashes for specific keys
by Corion (Patriarch) on Oct 21, 2016 at 09:50 UTC

    You want to push a hash reference and not a hash:

    push @filtered_responsearr, \%filtered_response;

    Your code can be greatly simplified in some other places:

    my $response = shift; my @fields; foreach my $a (0..$#_) { $fields[$a]= shift; }

    can be rewritten as a single assignment:

    my( $response, @fields ) = @_;

    Likewise, copying the elements of %response into the filtered response can be done using a hash slice instead of the loop:

    @filtered_response{ @fields } = @response{ @fields }

      %filtered_response is declared only once in the function call, so every reference to it will be identical. { %filtered_response } would work, but it would be better to move the declaration of %filtered_response inside the for loop.

      Or, with a recent enough version of perl, you can use a pairwise slice: push @filtered_responsearr, { %response{ @fields } };.
      With a postfix dereference: push @filtered_responsearr, { $res->%{ @fields } }

      Edit: I added the missing { } in the last line, to have a hash reference and not pairs of values.

      Wow, thanks for the tips, didn't know about hash slice. I'm just getting started so please forgive me my noobness. I tried what you said but it just gave me the same hash over and over, but Eily's answer solved that as well. Thanks to you both!
Re^3: Filter array of hashes for specific keys
by rsFalse (Chaplain) on Oct 28, 2016 at 09:26 UTC
    foreach my $a (0..$#_) { $fields[$a]= shift; }
    Avoid using $a and $b as variables, give them other names ;) .
    perlvar -> $a, $b

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-23 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found