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


in reply to inverting hash / grouping values

G'day LanX,

Here's a couple more to throw into the mix for you to reject. :-)

#!/usr/bin/env perl use strict; use warnings; my %h = (a => 1, b => 2, c => 1, d => 2, e => 1, f => 2); my %i; $i{$h{$_}}[@{ $i{$h{$_}} }] = $_ for keys %h; my %j = do { $_{$h{$_}}[@{$_{$h{$_}}}] = $_ for keys %h; %_ }; use Data::Dump; dd \%h, \%i, \%j;

Output:

$ pm_hash_invert.pl ( { a => 1, b => 2, c => 1, d => 2, e => 1, f => 2 }, { 1 => ["a", "e", "c"], 2 => ["f", "b", "d"] }, { 1 => ["a", "e", "c"], 2 => ["f", "b", "d"] }, )

-- Ken