[...] my @arrays = (\@array1, \@array2, [...]); my @union; my @intersection; my @difference; my %counts; foreach my $array (@arrays) { $counts{$_}++ foreach (@$array); } foreach my $item (keys %counts) { push @union, $item; push @{ $counts{$item} == scalar(@arrays) ? \@intersection : \@difference }, $item; }