http://www.perlmonks.org?node_id=164633


in reply to Reorganising a Hash for Output

Try this...
#!/usr/bin/perl -w use strict; my %x = ( 1 => [qw(d e f)], 2 => [qw(a b c)], 3 => [qw(g h i)] ); my %rev = map { my $key=$_; map { $_=>$key} @{$x{$key}} } keys %x; print "$_ -> $rev{$_}\n" foreach sort keys %rev;

The trick (imho) is to build the "reverse" hash first and then deal with sorting / outputing that after the fact.

Btw, I'd suggest not using non-standard notation, even if it's comfortable for you. It'll (potentially) make things harder when you're trying to show other people.