The following performs as required, though the worst-case scenario can involve up to N(N-1)/2 domain comparisons. I hope you don't have more than a few thousand domains to do this with.
use strict; use warnings; my (%domains, @domains, @sets, $i, $j); %domains = ( 'domain1.com' => ['company1', 'contact1', 'address1', 'phone1', 'fax1', 'email1 +'], 'domain2.com' => ['company2', 'contact2', 'address2', 'phone1', 'fax2', 'email1 +'], 'domain3.com' => ['company3', 'contact3', 'address3', 'phone3', 'fax3', 'email3 +'], 'domain4.com' => ['company3', 'contact4', 'address4', 'phone3', 'fax1', 'email1 +'], 'domain5.com' => ['company5', 'contact5', 'address5', 'phone5', 'fax5', 'email5 +'], 'domain6.com' => ['company5', 'contact6', 'address6', 'phone6', 'fax6', 'email5 +']); @domains = keys %domains; while ($#domains > -1) { my @newset = shift @domains; for ($i = 0; $i <= $#newset; $i++) { for ($j = 0; $j <= $#domains; $j++) { push @newset, splice(@domains, $j--, 1) if compare($newset[$i], $domains[$j]); } } push @sets, \@newset; } for (@sets) { print join ' ', sort @$_; print "\n"; } sub compare { my $d1 = $domains{$_[0]}; my $d2 = $domains{$_[1]}; my $c = 0; for (0..$#$d1) { return 1 if $d1->[$_] && $d1->[$_] eq $d2->[$_] && ++$c == 2; } }

In reply to Re: Building "islands" of related data by TedPride
in thread Building "islands" of related data by japhy

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.