my %hash2; for (@ary) { $hash2{$_} = $hash1{$_} if exists $hash1{$_}; } undef %hash1;