Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: inverting keys in a 2-D Hash

by 1nickt (Canon)
on Aug 30, 2015 at 14:21 UTC ( [id://1140438]=note: print w/replies, xml ) Need Help??


in reply to inverting keys in a 2-D Hash

I can't imagine doing this myself after so carefully constructing a hash to index my data, but, this will do what you want:

foreach my $key ( keys %hash ) { while ( my ( $subkey, $value ) = each %{ $hash{$key} } ) { $hash{ $subkey }{ $key } = $value; } delete $hash{ $key }; }

But what about when there are multiple occurrences of keyB in the original hash? Or multiple subkeys in the subhashes?

#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; my %hash; $hash{'keyA'}{'keyB'} = '1234'; $hash{'keyC'}{'keyB'} = '5678'; say Dumper \%hash; foreach my $key ( keys %hash ) { while ( my ( $subkey, $value ) = each %{ $hash{$key} } ) { $hash{ $subkey }{ $key } = $value; } delete $hash{ $key }; } say Dumper \%hash; __END__
Output:
$VAR1 = { 'keyA' => { 'keyB' => '1234' }, 'keyC' => { 'keyB' => '5678' } }; $VAR1 = { 'keyB' => { 'keyC' => '5678', 'keyA' => '1234' } };
.... is that really what you want?

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: inverting keys in a 2-D Hash
by melmoth (Acolyte) on Aug 30, 2015 at 22:01 UTC
    Yes this is exactly what I want. I have rna sequences that have been aligned to dna so each rna has associated with it a number of dna but other rna sequences may also align to some of these dna sequences. The original keys tell me what dna is aligned to each rna but i want to see, for each dna, what rna it has been aligned to.
      #!/usr/bin/perl use strict; use warnings; use feature qw/ say /; my %hash; $hash{'rna1'}{'dna1'} = '1111'; $hash{'rna1'}{'dna2'} = '1111'; $hash{'rna2'}{'dna1'} = '2222'; $hash{'rna3'}{'dna1'} = '2222'; $hash{'rna3'}{'dna2'} = '4444'; $hash{'rna2'}{'dna3'} = '3333'; say 'Report by RNA:'; foreach my $rna ( sort keys %hash ) { say " $rna has:"; foreach my $dna ( sort keys %{$hash{$rna}} ) { say " $dna = $hash{$rna}{$dna}"; } } say ''; # reverse the hash my %hash2 = %hash; foreach my $key ( keys %hash2 ) { while ( my ( $subkey, $value ) = each %{ $hash2{$key} } ) { $hash2{ $subkey }{ $key } = $value; } delete $hash2{ $key }; } say 'Report by DNA:'; foreach my $dna ( sort keys %hash2 ) { say " $dna is in:"; foreach my $rna ( sort keys %{$hash2{$dna}} ) { say " $rna with $hash2{$dna}{$rna}"; } } say ''; __END__
      $ perl 1140433.pl Report by RNA: rna1 has: dna1 = 1111 dna2 = 1111 rna2 has: dna1 = 2222 dna3 = 3333 rna3 has: dna1 = 2222 dna2 = 4444 Report by DNA: dna1 is in: rna1 with 1111 rna2 with 2222 rna3 with 2222 dna2 is in: rna1 with 1111 rna3 with 4444 dna3 is in: rna2 with 3333 $
      The way forward always starts with a minimal test.
Re^2: inverting keys in a 2-D Hash
by melmoth (Acolyte) on Aug 30, 2015 at 21:49 UTC
    Thanks everyone some good ideas

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140438]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found