I've got a data munging problem that is confusing me. I have a list of domain names and their associated WHOIS data (company, contact, address, phone, fax, email). I want to group domains together if they share at least two of those values. So if abc.com's company and phone are the same as def.com's company and phone, they should be in a group (array/hash, not important). But I also want to group xyz.com with abc.com and def.com if xyz.com shares two values with abc.com or def.com, and they needn't be the same fields as before. So if xyz.com's email and address are the same as def.com's email and address, it should be lumped together with abc.com and def.com.

Right now, I've got a bunch of data structures, but I don't know how to allocate this data into the "islands" of related domains. The word island is used because, when this is all done, the groups of domains will not have two values in common with any other groups.

My data structures are:

So, can anyone give me a good starting point? The killer is that the relationship has to be on TWO fields.

UPDATE

I think I've found a very different approach. I'm going through the list of domain names and building a hash $link{$d1}{$d2} = $degree_of_linkage based on the number of fields domains $d1 and $d2 have in common. Then I can use that to build my islands.

UPDATE

Here's the entirety of my code:
#!/usr/bin/perl use strict; use warnings; chomp(my @fields = split /\t/, <>); my (%domains, %links, %rel, %group); shift @fields; while (<>) { chomp; my ($dom, @values) = split /\t/, $_, -1; @{ $domains{$dom} }{@fields} = @values; } my @dlist = my @domain_list = keys %domains; while (@dlist) { my $d1 = shift @dlist; for my $d2 ($d1, @dlist) { $links{$d1}{$d2} = [ grep { $domains{$d1}{$_} eq $domains{$d2}{$_} and $domains{$d1}{$_} ne "" and $domains{$d1}{$_} ne "Private, Registration" and $domains{$d1}{$_} ne "Domains by Proxy, Inc." and $domains{$d1}{$_} !~ /^DomainsByProxy.com/i } @fields ]; } } for my $d1 (@domain_list) { $rel{$d1} ||= $d1; $group{ $rel{$d1} }{$d1} = 1; for my $d2 (grep { @{ $links{$d1}{$_} } > 1 } keys %{ $links{$d1} }) + { $rel{$d2} ||= $d1; $group{ $rel{$d2} }{$d2} = 1; } } for my $d1 (sort { keys(%{ $group{$b} }) <=> keys(%{ $group{$a} }) } k +eys %group) { print "GROUPED TO $d1\n"; for my $d2 (sort keys %{ $group{$d1} }) { print " $d2 (via: @{ $links{$d1}{$d2} })\n"; } }

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

In reply to 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.