foreach my $keylogt (keys(%hash1)){ foreach my $keyp (keys(%hash2)){ my @allkeys=(keys %{$hash2{$keyp}},keys %{$hash1{$keylogt}}); my %unique; map($unique{$_}++,@allkeys); my @uniquekeys=keys %unique; # now you have a list of ALL keys in both hashes # and you can process each one and determine if # it appears in one, the other, or both.. } }