It will be be good to see what you have tried in first place

but I won't stop with that

Here is one way of doing it
input hash

my %hash= ( Group1 => 'ATG1,ATG2,ATG4,ATRG7', Group2 => 'ATG1,ATG9', Group3 => 'FYCO1,LSM2', Group4 => 'ATG1,MAM2', Group5 => 'LSM2', );
code
use strict; use warnings; use Data::Dumper; my %hash= ( Group1 => 'ATG1,ATG2,ATG4,ATRG7', Group2 => 'ATG1,ATG9', Group3 => 'FYCO1,LSM2', Group4 => 'ATG1,MAM2', Group5 => 'LSM2', ); my (%newhash,$i,%combinegroups,%splittedhash); foreach my $key (keys %hash){ my @values ; if($hash{$key} =~ /,/){ @values = split(/,/,$hash{$key}) ; $splittedhash{$key} = [ @values ]; }else{ push(@values,$hash{$key}); $splittedhash{$key} = [ @values ]; } if(++$i == 1) { my @newvalues; push (@newvalues,$key) for(0..scalar @values); @newhash{@values} = @newvalues; }else{ foreach (@values){ if(exists $newhash{$_}){ $combinegroups{$newhash{$_}.":".$key}++; }else{ $newhash{$_} = $key; } } } } foreach my $key (keys %combinegroups){ my ($firstgroup,$secondgroup) = split(/:/,$key); my %unique; @unique{@{$splittedhash{$firstgroup}},@{$splittedhash{$secondgroup +}}} = (); $hash{$firstgroup} = join(',',keys %unique); @{$splittedhash{$firstgroup}} = keys %unique; delete $hash{$secondgroup}; } print "FINAL HASH\n",Dumper \%hash;
output
FINAL HASH $VAR1 = { 'Group4' => 'ATG9,ATG2,ATRG7,ATG4,ATG1,MAM2', 'Group5' => 'FYCO1,LSM2' };
but, this code will behave wrongly for a condition, if value1 in group1 match with group2 and at the same time value2 in group1 match with group3.
might have some other bugs also
but to start with, this code will work, the rest is upto you

I haven't worried about efficiency in my code, it can be written even more effectively!, but this will work for your this case
if a value from 1 group (key) is found as a value from another group then I want to combine the values from both of those groups into a single set of values


Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

In reply to Re: Should I use a hash for this? by targetsmart
in thread Should I use a hash for this? by awos22

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.