Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am doing research work. I want to classify my context. I made a hash, the keys are ids and the values are descriptions. For the values part, the same case they use a couple of ways to descrip. like: if the value match 'ABC' and 'EDF' or match 'CDE' or match 'EDJ' and 'KIJ', then they will all belong to Case A group, I will put all those keys in one group. I made my hash:
...; @id=...; foreach(@id){ my @junkdesc=...; foreach(@junkdesc){ my @data=split('..', $_); $desc = $data[2]; } $myhash{$_}=$desc; }
How to do that? Please help!!! and Thanks in advance!!!

Replies are listed 'Best First'.
Re: How to group keys by hash value
by broquaint (Abbot) on Apr 25, 2003 at 10:09 UTC
    A hash of hashes should do the trick
    use Data::Dumper; my %hash = ( ABC => 'foo', DEF => 'bar', XXX => 'baz', YYY => 'quux', ); my %casehash; for(keys %hash) { $casehash{CaseA}->{$_} = $hash{$_} if /^[A-F]+\z/; $casehash{CaseZ}->{$_} = $hash{$_} if /^[W-Z]+\z/; } print Dumper(\%casehash); __output__ $VAR1 = { 'CaseA' => { 'ABC' => 'foo', 'DEF' => 'bar' }, 'CaseZ' => { 'XXX' => 'baz', 'YYY' => 'quux' } };
    You might want to setup another hash to map your ids to their appropriate case groups. See. perldsc for more info on data structures in perl.
    HTH

    _________
    broquaint

      You can combine it into one hash.
      use Data::Dumper; $hash{ABC}{desc} = "foo"; $hash{DEF}{desc} = "bar"; $hash{XYZ}{desc} = "baz"; $hash{YYY}{desc} = "quzz"; foreach $key (keys %hash){ if ($hash{$key}{desc} =~ /^[foo|bar]+\z/) { $hash{$key}{group} = "CaseA" ; } if ($hash{$key}{desc} =~ /^[baz|quzz]+\z/){ $hash{$key}{group} = "CaseZ" ; } } print Dumper(\%hash); __output__ $VAR1 = { 'ABC' => { 'group' => 'CaseA', 'desc' => 'foo' }, 'DEF' => { 'group' => 'CaseA', 'desc' => 'bar' }, 'XYZ' => { 'group' => 'CaseZ', 'desc' => 'baz' }, 'YYY' => { 'group' => 'CaseZ', 'desc' => 'quzz' } };
      Cheers ^_^
        Thanks for the help! I try to use it to my problem, I still have trouble, it seems only one problem or so, but I have no idea to solve it, please give hands again!!
        $ perl lxsubcp.pl Bareword "GTPase" not allowed while "strict subs" in use at lxsubcp.pl + line 105. Bareword "protein" not allowed while "strict subs" in use at lxsubcp.p +l line 106. $hash{GTPase-activating proteins}{domaindescription} = "G01"; $hash{protein kinase A anchoring protein}{domaindescription} = "K01"; #line 105 and 106 Bareword "G" not allowed while "strict subs" in use at lxsubcp.pl line + 112. $hash{G-protein-coupled receptor kinase}{domaindescription} = "G05"; Bareword "protein" not allowed while "strict subs" in use at lxsubcp.p +l line 112. Bareword "kinase" not allowed while "strict subs" in use at lxsubcp.pl + line 112. Bareword "G" not allowed while "strict subs" in use at lxsubcp.pl line + 121. $hash{G-alpha GTPase}{domaindescription} = "G07"; Bareword "G" not allowed while "strict subs" in use at lxsubcp.pl line + 123. Bareword "protein" not allowed while "strict subs" in use at lxsubcp.p +l line 123. Bareword "proteins" not allowed while "strict subs" in use at lxsubcp. +pl line 124. Bareword "protein" not allowed while "strict subs" in use at lxsubcp.p +l line 129. Bareword "Serine" not allowed while "strict subs" in use at lxsubcp.pl + line 138. Bareword "G01" not allowed while "strict subs" in use at lxsubcp.pl li +ne 143. if ($hash{$key}{domaindescription} =~ /^[G01|P02|R03|A01|G07|I01|I02|I06]+\Z){ $hash{$key}{group} = "LGN"; } Bareword "G08" not allowed while "strict subs" in use at lxsubcp.pl li +ne 143. Bareword "G05" not allowed while "strict subs" in use at lxsubcp.pl li +ne 143. Bareword "G09" not allowed while "strict subs" in use at lxsubcp.pl li +ne 143. Bareword "I03" not allowed while "strict subs" in use at lxsubcp.pl li +ne 143. Bareword "Z" not allowed while "strict subs" in use at lxsubcp.pl line + 143. Bareword "Z" not allowed while "strict subs" in use at lxsubcp.pl line + 150.<code> if ($hash{$key}{domaindescription} =~ /^[R01|R07|I04|R08|G02|K02|R05|P02|R02|I04|C02|R06|C03|S01|S02|E01]+\ +Z){ $hash{$key}{group} = "RhoGEF"; } Execution of lxsubcp.pl aborted due to compilation errors.
Re: How to group keys by hash value
by JamesNC (Chaplain) on Apr 25, 2003 at 14:03 UTC
    ... or use Class::Struct;
    package record; use Data::Dumper; use Class::Struct; my $i = 0; struct( first_name => '$', last_name => '$', ssn => '$', nicknames => '@', ); my $person = new record; $person->first_name("John"); $person->last_name("Doe"); $person->ssn("123-45-6789"); my @nicks = qw( jester ice-man maverick ); $person->nicknames($i++,$_) for @nicks; print Dumper(\$person); print "@{$person->nicknames}";
    lots of ways to do it.. cause hey, it's Perl :-) JamesNC