in reply to How to group keys by hash value

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

Replies are listed 'Best First'.
Re: Re: How to group keys by hash value
by donno20 (Sexton) on Apr 25, 2003 at 11:49 UTC
    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.