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

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 }

Replies are listed 'Best First'.
Re^4: Filter array of hashes for specific keys
by Eily (Monsignor) on Oct 21, 2016 at 10:00 UTC

    %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.

Re^4: Filter array of hashes for specific keys
by ddominnik (Novice) on Oct 21, 2016 at 10:40 UTC
    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!