foreach my $i (@a) { foreach my $j (@b) { if ($i eq $j) { undef $i; undef $j; last; } } }