7stud has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
Suppose you have two parallel arrays:
my @keys = qw {red blue red}; my @vals = (1, 2, 3);
Does perl have an idiom to end up with this:
my %hash = ( red => [1, 3], blue => [2], )
That is, does perl have an idiom for: create a key if it doesn't exist and assign it a default empty array then push the current integer onto the array; else if the key already exists push the current integer onto its corresponding array?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: idiom for building hash of arrays?
by CountZero (Bishop) on Jan 23, 2010 at 12:07 UTC | |
Re: idiom for building hash of arrays?
by FunkyMonk (Chancellor) on Jan 23, 2010 at 12:29 UTC | |
Re: idiom for building hash of arrays?
by ww (Archbishop) on Jan 23, 2010 at 12:02 UTC | |
Re: idiom for building hash of arrays?
by Anonymous Monk on Jan 23, 2010 at 15:32 UTC | |
Re: idiom for building hash of arrays?
by Anonymous Monk on Jan 23, 2010 at 11:10 UTC | |
by 7stud (Deacon) on Jan 23, 2010 at 11:15 UTC | |
by Anonymous Monk on Jan 23, 2010 at 11:30 UTC |
Back to
Seekers of Perl Wisdom