#!/usr/bin/perl -w use strict; use warnings; my @array1 = ( "test", "test2", "test2", "test3", "test4", "test4" ); my @array2 = ( "test", "test", "test2.1", "test4.1", "test4.2", "test4.3" ); my @save; foreach my $item1 (@array1) { foreach my $item2 (@array2) { next unless defined $item2; if (match_names($item1, $item2)) { push (@save, $item1); $item2 = undef; last; } } } printf "Results: save = %s\n", join(',', @save); sub match_names { # i have this part, its just a simple regex # something like: my ($x,$y) = @_; return 1 if ($y =~ /$x/); return 0; }