@array1 = [ "test", "test2", "test2", "test3", "test4", "test4" ]; @array2 = [ "test", "test", "test2.1", "test4.1", "test4.2", "test4.3" ]; #### 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/); } #### @save contains [ "test", "test2", "test4", "test4" ];