Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: inverting hash / grouping values

by kcott (Abbot)
on Sep 28, 2013 at 15:10 UTC ( #1056141=note: print w/ replies, xml ) Need Help??


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


Comment on Re: inverting hash / grouping values
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2015-07-04 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls