in reply to [Answered!] Taking a subset of a hash

I would probably have written something like:

use strict; use warnings; use Data::Dumper; my %hash = ( a => 1, b => 0, c => undef, e => 1, f => 0, g => undef ); my @interesting_keys = qw( a b c d ); my %small_hash = map { ($hash{$_}) ? ($_ => $hash{$_}) : () } @interesting_keys; print Dumper(\%small_hash);

but maybe the following is easier to understand...

my %small_hash = map { $_ => $hash{$_} } grep { $hash{$_} } @interesting_keys;

Both give the same result:

$VAR1 = { 'a' => 1 };

which is what you are wanting, if I understood correctly.

These are only a little different from some of the other suggestions you have received and I don't suggest they are necessarily any better. Which is best for you depends on what you and the others that might read you code know of Perl. You should choose a solution which you find easy to understand and modify or re-use as your needs evolve.