Hi everyone, I'm working on a bacterial neighbor grouping problem. Basically the idea is there are many strains of a particular type of bacteria and we are looking at grouping 'sets' of these together based on relatedness (i.e. Bacteria_1 is more closely related to Bacteria_2 than Bacteria_3 because 1 and 2 have many more genes in common than 3). I have gotten to the point where I have 2d hashes representing all the groups I've found. However some of these groups overlap, for example:

%groupOfBact

looks like:

group1=>()

group2=>()

group3=>()

I would like to take %groupOfBact and merge the groups with common strains, while leaving the ones with no common strains intact. So, after the operation %groupOfBact would look like

group1=>()

group2=>()

I don't really care about what the values are afterwords (i.e. strain6=>10000 for all I care) and I think I have a structure that kind of works... but I feel like its really clunky, and even worse almost impossible for other people to read and understand. hrm, here's what it looks like:
#Handle the case of overlapping groups $groupID = 1; my %groupSeen; foreach my $group1 (keys %groupOfBact){ foreach my $group2 (keys %groupOfBact){ if ($group1 != $group2){ foreach my $strain (keys %{$groupOfBact{$group1}}){ if((exists $groupOfBact{$group2}{$strain})&&(!exists $ +groupSeen{$strain})){ %{$updatedGroupHash{$groupID}} = (%{$groupOfBact{$ +group1}}, %{$groupOfBact{$group2}}); foreach my $seen (keys %{$updatedGroupHash{$groupI +D}} ){ $groupSeen{$seen}=1; } $groupID++; last; } else { %{$updatedGroupHash{$groupID}} = %{$groupOfBact{$g +roup1}}; } } } } }
Any help would be greatly appreciated! Oh, I've also taken a look at Hash::Merge, but it seems to break with some message about odd numbers of elements... I could be using it incorrectly however (I'm still pretty new to perl)

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