foreach my $key (keys %hash1) { if (exists $hash2{$key}) { print "$key\n"; } } close IN;