<?xml version="1.0" encoding="windows-1252"?>
<node id="1007898" title="Re: I need help with some recursion" created="2012-12-08 10:42:37" updated="2012-12-08 10:42:37">
<type id="11">
note</type>
<author id="968231">
Athanasius</author>
<data>
<field name="doctext">
&lt;p&gt;Hello [ragnarokPP], and welcome to the Monastery!&lt;/p&gt;

&lt;p&gt;[tobyink] has shown you how to solve the problem with recursion, as you requested. Here is a non-recursive solution using the [mod://] module [mod://Set::Scalar]:&lt;/p&gt;

&lt;code&gt;
#! perl
use Modern::Perl;
use Set::Scalar;

my $class_callback = sub { join(' ', sort { $a &lt;=&gt; $b } $_[0]-&gt;elements) };
Set::Scalar-&gt;as_string_callback($class_callback);

my @sets;

for (my $i = 0; &lt;DATA&gt;;)
{
    chomp;
    my $new_set = Set::Scalar-&gt;new(split /\s+/);
    my $merged  = 0;

    for my $j (0 .. $i - 1)
    {
        if ($new_set-&gt;intersection($sets[$j]))
        {
            $sets[$j] = $sets[$j]-&gt;union($new_set);
            $merged   = 1;
            last;
        }
    }

    $sets[$i++] = $new_set unless $merged;
}

print '(', $_, ")\n" for @sets;

__DATA__
1 2 4 
2 3 4
3 7
4 6
5 10 11 12 13
6
7 1
&lt;/code&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;code&gt;
 1:30 &gt;perl 424_SoPW.pl
(1 2 3 4 6 7)
(5 10 11 12 13)

 1:34 &gt;
&lt;/code&gt;

&lt;p&gt;Remember, the Perl motto is TMTOWTDI (There&amp;rsquo;s More Than One Way To Do It)!&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; The above code doesn&amp;rsquo;t merge fully on certain types of input. The following code fixes this:&lt;/p&gt;

&lt;code&gt;
#! perl
use Modern::Perl;
use Set::Scalar;

my $class_callback = sub { join(' ', sort { $a &lt;=&gt; $b } $_[0]-&gt;elements) };
Set::Scalar-&gt;as_string_callback($class_callback);

my @sets;

for (my $i = 0; &lt;DATA&gt;; ++$i)
{
    chomp;
    $sets[$i] = Set::Scalar-&gt;new(split /\s+/);
}

print "Before merging:\n";
print '(', $_, ")\n" for @sets;
print "\n";

for my $i (reverse 1 .. $#sets)
{
    for my $j (0 .. $i - 1)
    {
        if (defined $sets[$i] &amp;&amp;
            defined $sets[$j] &amp;&amp;
            $sets[$i]-&gt;intersection($sets[$j]))
        {
            $sets[$j] = $sets[$i]-&gt;union($sets[$j]);
            $sets[$i] = undef;
        }
    }
}

@sets = grep { defined } @sets;

print "After merging:\n";
print '(', $_, ")\n" for @sets;

__DATA__
1 2 4
7 13
3 5 6
7 8
10 11 12
1 5
&lt;/code&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;code&gt;
11:24 &gt;perl 424_SoPW.pl
Before merging:
(1 2 4)
(7 13)
(3 5 6)
(7 8)
(10 11 12)
(1 5)

After merging:
(1 2 3 4 5 6)
(7 8 13)
(10 11 12)

12:17 &gt;
&lt;/code&gt;

&lt;p&gt;Hope that helps,&lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-968231"&gt;
&lt;p&gt;
&lt;table width="100%"&gt;
&lt;tr&gt;
  &lt;td align="left"&gt;
    Athanasius&amp;emsp;&lt;font color="#008000"&gt;&amp;lt;&lt;/font&gt;[href://http://www.biblegateway.com/passage/?search=John%203:16&amp;version=NLV|&lt;font color="#008000"&gt;&amp;deg;&lt;/font&gt;]&lt;font color="#008000"&gt;(((&amp;gt;&amp;lt;&lt;/font&gt;&amp;emsp;&lt;i&gt;contra mundum&lt;/i&gt;
  &lt;/td&gt;
  &lt;td align="right"&gt;
    [href://http://translate.google.com.au/#la/en/Iustus%20alius%20egestas%20vitae%2C%20eros%20Piratica%2C|&lt;b&gt;Iustus alius egestas vitae, eros Piratica,&lt;/b&gt;]
  &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1007887</field>
<field name="parent_node">
1007887</field>
</data>
</node>
