spurperl has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
A few minutes ago I needed to "fold" an array into a hash representing a bag/set. That is, from [a, b, c] I wanted to create {a => 1, b => 1, c => 1}. Without thinking much, I slapped:
my %bag = map {($_, 1)} @arr;
And now I wonder on the merits of this technique. I recall seeing it discussed in the monastery before, I can't don't know what to look for in the Supersearch. Any fellow monks with memory better than mine ?
What I want to know, basically, is whether it's a Good Way (TM), what are the alternatives, etc.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using map to create a hash/bag from an array
by GrandFather (Saint) on Dec 17, 2005 at 10:41 UTC | |
by bart (Canon) on Dec 18, 2005 at 10:45 UTC | |
by GrandFather (Saint) on Dec 18, 2005 at 11:21 UTC | |
Re: Using map to create a hash/bag from an array
by GrandFather (Saint) on Dec 17, 2005 at 11:21 UTC | |
by shotgunefx (Parson) on Dec 18, 2005 at 08:34 UTC | |
Re: Using map to create a hash/bag from an array
by jdporter (Paladin) on Dec 17, 2005 at 14:19 UTC | |
Re: Using map to create a hash/bag from an array
by osunderdog (Deacon) on Dec 17, 2005 at 10:50 UTC | |
by mrborisguy (Hermit) on Dec 18, 2005 at 04:22 UTC |
Back to
Seekers of Perl Wisdom