fork download
  1. use strict;
  2. use warnings;
  3.  
  4. my %hash = (
  5. one => 1,
  6. two => 2,
  7. four => 4,
  8. );
  9.  
  10. my @required_keys = qw(two three four);
  11. my %result_hash = map { $_ => $hash{$_} if (exists $hash{$_}) } @required_keys;
  12.  
  13. use Data::Dumper;
  14. print 'Resulting hash: ' . Dumper(\%result_hash);
  15.  
Success #stdin #stdout 0.02s 5060KB
stdin
Standard input is empty
stdout
Resulting hash: $VAR1 = {
          '' => 'four',
          '4' => undef,
          'two' => 2
        };