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

In reply to Re^3: inverting keys in a 2-D Hash by 1nickt
in thread inverting keys in a 2-D Hash by melmoth

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.