my %temp_for_compare; # Add values from the first array into the temporary hash $temp_for_compare{$_} = 0 for @array1; # Now we can simply check the items in the second array against the hash for my $val (@array2) { if (exists $temp_for_compare{$val}) { print "$val in both arrays\n"; } }