#!/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; } #### my @array1 = [ "A", "B", "C" ]; #### use strict; use warnings; use Data::Dumper; my @array1 = [ "test", "test2", "test2", "test3", "test4", "test4" ]; printf "Contents of 'array1' => %s\n", Dumper(\@array1); my @array2 = ( "test", "test2", "test2", "test3", "test4", "test4" ); printf "Contents of 'array2' => %s\n", Dumper(\@array2); # Output will be as follows ... Contents of 'array1' => $VAR1 = [ [ 'test', 'test2', 'test2', 'test3', 'test4', 'test4' ] ]; Contents of 'array2' => $VAR1 = [ 'test', 'test2', 'test2', 'test3', 'test4', 'test4' ]; #### return 1 if ($y =~ /$x/); # Swapped $x and $y #### my @array2_copy = @array2; foreach my $item1 (@array1) { foreach my $item2 (@array2_copy) { ... } }