foreach my $key (keys %hash1) { if(exists($hash2{$key}) { if($hash1{$key} == $hash2{$key}) # '==' if numeric, 'eq' if string { ... } else { ... } } else # this is the case you missed - $key not in file2 { ... } }