Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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 musing on the Monastery: (5)
As of 2014-09-21 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls