my @A = [ '1', '2', '4', '8' ]; my @B = [ '1', '3', '6', '8' ]; my @C; foreach my $A (@A) { foreach my $B (@B) { if ($A == $B) { push @C, $A; } // something here to take the elements out? } }