This is a somewhat tricky problem because later data may require that separate groups that were formed by earlier data need to be merged. Where the code is a little tricky it's worth being a little long winded, use explicit identifier names and comment each case. Consider:

use strict; use List::Util; my %nodes; my @groups; while (<DATA>) { chomp; my ($node1, $node2) = split; if (! exists $nodes{$node1} && ! exists $nodes{$node2}) { # New group $nodes{$node1} = @groups; $nodes{$node2} = @groups; push @groups, [$node1, $node2]; next; } if (! exists $nodes{$node1}) { # node1 is part of node2's group push @{$groups[$nodes{$node2}]}, $node1; $nodes{$node1} = $nodes{$node2}; } if (! exists $nodes{$node2}) { # node2 is part of node1's group push @{$groups[$nodes{$node1}]}, $node2; $nodes{$node2} = $nodes{$node1}; } next # Already met this pairing if $nodes{$node1} == $nodes{$node2}; # node1 and node2 are in different groups. Merge the groups my ($group, $nulGroup) = ($nodes{$node1}, $nodes{$node2}); push @{$groups[$group]}, @{$groups[$nulGroup]}; $nodes{$_} = $group for @{$groups[$nulGroup]}; $groups[$nulGroup] = undef; } @groups = grep {defined} @groups; for my $group (0 .. $#groups) { print 'Group', $group + 1, ': ', join (', ', @{$groups[$group]}), +"\n"; } __DATA__ Contig1 Contig2 Contig1 Contig3 Contig2 Contig1 Contig2 Contig3 Contig3 Contig1 Contig3 Contig2 Contig3 Contig4 Contig4 Contig3 Contig4 Contig5 Contig6 Contig7 Contig7 Contig6 Contig8 Contig9 Contig9 Contig10 Contig10 Contig8 Contig10 Contig11 Contig11 Contig10 C12 Contig11 C12 Contig5

Prints:

Group1: Contig6, Contig7 Group2: Contig8, Contig9, Contig10, Contig11, C12, Contig1, Contig2, C +ontig3, Contig4, Contig5
True laziness is hard work

In reply to Re: Finding all connected nodes in an all-against-all comparison by GrandFather
in thread Finding all connected nodes in an all-against-all comparison by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.