You could use Algorithm::EquivalenceSets.

#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; use Algorithm::EquivalenceSets; my %groupOfBact = ( group1 => { strain1=>1, strain2=>1, strain4=>1, } , group2 => { strain3=>1, strain4=>1, strain5=>1, } , group3 => { strain6=>1, strain7=>1, }, ); print Dumper(\%groupOfBact); my @sets = map { [ keys %{$groupOfBact{$_}} ] } keys %groupOfBact; print Dumper(\@sets); my @equiv_sets = equivalence_sets(\@sets); print Dumper(\@equiv_sets);

produces

$VAR1 = { 'group2' => { 'strain5' => 1, 'strain3' => 1, 'strain4' => 1 }, 'group1' => { 'strain2' => 1, 'strain4' => 1, 'strain1' => 1 }, 'group3' => { 'strain7' => 1, 'strain6' => 1 } }; $VAR1 = [ [ 'strain5', 'strain3', 'strain4' ], [ 'strain2', 'strain4', 'strain1' ], [ 'strain7', 'strain6' ] ]; $VAR1 = [ [ 'strain7', 'strain6' ], [ 'strain2', 'strain4', 'strain1', 'strain5', 'strain3' ] ];

There are other modules that might be helpful, depending on what you are wanting to do: Graph::TransitiveClosure, Graph::AdjacencyMatrix, etc.

update: Here is an example using Graph:

#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; use Graph; my %groupOfBact = ( group1 => { strain1=>1, strain2=>1, strain4=>1, } , group2 => { strain3=>1, strain4=>1, strain5=>1, } , group3 => { strain6=>1, strain7=>1, }, ); print Dumper(\%groupOfBact); my $graph = Graph::Undirected->new; foreach my $group (keys %groupOfBact) { foreach my $strain (keys %{$groupOfBact{$group}}) { $graph->add_edge($group, $strain); } } my @cc = $graph->connected_components(); print Dumper(\@cc);

Which produces

$VAR1 = { 'group2' => { 'strain5' => 1, 'strain3' => 1, 'strain4' => 1 }, 'group1' => { 'strain2' => 1, 'strain4' => 1, 'strain1' => 1 }, 'group3' => { 'strain7' => 1, 'strain6' => 1 } }; $VAR1 = [ [ 'strain7', 'group3', 'strain6' ], [ 'strain3', 'group2', 'strain5', 'strain4', 'group1', 'strain1', 'strain2' ] ];

The Graph module gives you many options for entering your data and analyzing it.


In reply to Re: Merging Complex Hashes by ig
in thread Merging Complex Hashes by jpearl

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.