Last active
June 22, 2016 15:25
-
-
Save nicomen/d779393767370c136c46917e6b073e9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Data::Dumper; | |
my @interesting_keys = qw/a/; | |
my $hash_list = [ { a => 1, b => 1 }, { a => 2 } ]; | |
my $new_hash_list = [ | |
map { | |
my $dest = $_; | |
+{ | |
map { $_ => $dest->{$_} } @interesting_keys | |
}; | |
} @{$hash_list} | |
]; | |
print Dumper($new_hash_list); | |
my $new_hash_list2 = []; | |
for my $dest (@{$hash_list}) { | |
my $new_dest; | |
@{$new_dest}{@interesting_keys} = @{$dest}{@interesting_keys}; | |
push @{$new_hash_list2}, $new_dest; | |
} | |
print Dumper($new_hash_list2); | |
# in place editing! | |
my %is_interesting = map { $_ => 1 } @interesting_keys; | |
for my $dest (@{$hash_list}) { | |
for my $key (keys %{$dest}) { | |
delete $dest->{$key} unless $is_interesting{$key} | |
} | |
} | |
print Dumper($hash_list); | |
__END__ | |
$VAR1 = [ | |
{ | |
'a' => 1 | |
}, | |
{ | |
'a' => 2 | |
} | |
]; | |
$VAR1 = [ | |
{ | |
'a' => 1 | |
}, | |
{ | |
'a' => 2 | |
} | |
]; | |
$VAR1 = [ | |
{ | |
'a' => 1 | |
}, | |
{ | |
'a' => 2 | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment