foreach my $item (sort @array1) { foreach my $found (@array2) { if (&match_names($item,$found)) { print "found a match ($item = $found\n"; push(@save, $found); } # end-if } # end-foreach } # end-foreach foreach my $item (sort @array2) { foreach my $found (@array1) { if (&match_names($item,$found)) { print "found a match ($item = $found\n"; push(@save, $found); } # end-if } # end-foreach } # end-foreach sub match_names { # i have this part, its just a simple regex # BUT this must only match in "one direction": test4 matches test 4.1, but test4.1 doesnt match test4 return 1 if ($x =~ /some_regex_on_$y/); }