jaredor already pointed out the related post from 3 weeks ago where someone wanted to do the same thing: how to find combine common elements of an array?
However, after a little thought, I came up with a shorter solution than my original suggestion:
use strict; use warnings; #my @array = ([11,12], [11,13], [9,8,7], [3,4], [11,4]); my @array = ([0], [0,1], [0,1,2], [1,2,3], [6,4,5]); ORIG: for my $i (0..$#array) { for my $j ($i+1..$#array) { my %seen; my @unique = grep {! $seen{$_}++} @{$array[$i]}, @{$array[$j]} +; if (@unique < @{$array[$i]} + @{$array[$j]}) { $array[$i] = \@unique; splice @array, $j, 1; redo ORIG; } } } print join(' ', @$_), "\n" for (@array);
Note: This only works if each set contains unique elements only.
In reply to Re: How can sets be recursively concatenated when intersecting with each other
by wind
in thread How can sets be recursively concatenated when intersecting with each other
by joni
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |