http://qs321.pair.com?node_id=901038

joni has asked for the wisdom of the Perl Monks concerning the following question:

Hi there. I want to recursively concatenate (union) sets that are not disjointed (the intersected set is not empty). For example, if I have the sets

(0) (0 1) (0 1 2) (1 2 3) (6 4 5)

The output should be:

((0 1 2 3) (4 5 6)
This is the code when I compare iteratively sets one by one:
use Set::Scalar; for $i ( 0 .. 4) {$cluster[$i] = Set::Scalar->new;} $cluster[0]->insert(0); $cluster[1]->insert(0,1); $cluster[2]->insert(0,1,2); $cluster[3]->insert(1,2,3); $cluster[4]->insert(5,6,4); for $i ( 0 .. 4) { for $j ( $i+1 .. 4) { if ($cluster[$i]->is_properly_intersecting($cluster[$j])) {$cluste +r[$i]=$cluster[$i]+$cluster[$j];$cluster[$j]->clear;} }}
The output is (not what I want):
(0) (0 1 2 3) (0 1 2) () (4 5 6)