Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: inverting keys in a 2-D Hash

by 1nickt (Canon)
on Aug 31, 2015 at 05:29 UTC ( [id://1140500]=note: print w/replies, xml ) Need Help??


in reply to Re^2: inverting keys in a 2-D Hash
in thread inverting keys in a 2-D Hash

#!/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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found