Skip to content

Instantly share code, notes, and snippets.

@sudofox
Created November 30, 2022 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudofox/1657263e8f1aea427892ed48759c5a9c to your computer and use it in GitHub Desktop.
Save sudofox/1657263e8f1aea427892ed48759c5a9c to your computer and use it in GitHub Desktop.
filter an array of hashrefs for only the fields i want
# as function
sub filter { my $fields = shift; my @results = @_; foreach my $result (@results) { my $replacement = {}; foreach my $field (@$fields) { $replacement->{$field} = $result->{$field}; } $result = $replacement; } return @results; }
# as anonymous func
my $filter = sub {my $fields = shift; my @results = @_; foreach my $result (@results) {my $replacement = {}; foreach my $field (@$fields) {$replacement->{$field} = $result->{$field};} $result = $replacement;} return @results;};
# usages
my @results = $filter->([qw/metadata uuid/], $object->attribute->asHashRefs);
my @results = filter([qw/metadata uuid/], $object->attribute->asHashRefs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment