The key is the counts:use strict; use warnings; my (@sets, $n, %c, @u, @a); while (<DATA>) { chomp; push @sets, [split /,/]; } for (@sets) { $c{$_}++ for (@$_); } $n = $#sets + 1; for (@sets) { print '[' . join(',', @$_) . '] = ['; @u = (); @a = (); for (@$_) { if ($c{$_} != $n) { push(@u, [$_, $c{$_}]); } else { push (@a, $_); } } for ((sort {@$a[1] cmp @$b[1]} @u)[0..1]) { print @$_[0] . ','; } print $a[0] . "]\n"; } __DATA__ 1,2,3,4,5,6 3,4,5,7,8 3,6,8,9
This gives you the total number of times each value is used over the entire nested array. If the number is equal to the number of arrays, then the value is common to all arrays; if the number is 1, then the value is unique to whichever array contains it. Numbers in between can be used to fill in if there aren't enough 1's, as shown above.for (@sets) { $c{$_}++ for (@$_); }
I've assumed that there are always at least two values in each array not common to all arrays. I've also assumed that the arrays can contain non-numerical values, which is why I'm using cmp for the sort instead of <=>.
In reply to Re: Mutually Exclusive Elements
by TedPride
in thread Mutually Exclusive Elements
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |