ddominnik has asked for the wisdom of the Perl Monks concerning the following question:
Say I have an array of hashes like this:
@response = [ { 'name' => 'foo', 'id' => 'bar', 'self' => 'baz' }, { 'name' => 'foo2', 'id' => 'bar2', 'self' => 'baz2' }, ]
Now I have a subroutine called filter which I call like this:
my $responseref = \@response; my $filtered_response = filter ($responseref, 'name', 'id');
How would the subroutine need to look to get an array reference as return value of which the array would look like:
@response = [ { 'name' => 'foo', 'id' => 'bar', }, { 'name' => 'foo2', 'id' => 'bar2', }, ]
Ideally it should work without installing any additional modules
It should also be pretty generic, it should for example be able take a different amount of fields to filter through every time
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Filter array of hashes for specific keys
by Corion (Patriarch) on Oct 21, 2016 at 09:36 UTC | |
by ddominnik (Novice) on Oct 21, 2016 at 09:45 UTC | |
by Corion (Patriarch) on Oct 21, 2016 at 09:50 UTC | |
by Eily (Monsignor) on Oct 21, 2016 at 10:00 UTC | |
by ddominnik (Novice) on Oct 21, 2016 at 10:40 UTC | |
by rsFalse (Chaplain) on Oct 28, 2016 at 09:26 UTC | |
|
Re: Filter array of hashes for specific keys
by Athanasius (Archbishop) on Oct 22, 2016 at 03:12 UTC |