Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Using map to create a hash/bag from an array

by GrandFather (Sage)
on Dec 17, 2005 at 10:41 UTC ( #517462=note: print w/replies, xml ) Need Help??

in reply to Using map to create a hash/bag from an array

The idiomatic way to do that in Perl is @hash{qw(a b c)} = (1) x 3;:

use strict; use warnings; use Data::Dumper; my %hash; @hash{qw(a b c)} = (1) x 3; print Dumper (\%hash);


$VAR1 = { 'c' => 1, 'a' => 1, 'b' => 1 };

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Using map to create a hash/bag from an array
by bart (Canon) on Dec 18, 2005 at 10:45 UTC
    Except, IMHO, the hash values are irrelevant. Codewise, it's easier to use undef as hash values: your
    @hash{qw(a b c)} = (1) x 3;
    then becomes
    @hash{qw(a b c)} = ();

    That's a lot shorter, isn't it? And no need to count items.

      And faster too (making the obvious benchmark addition):

      Rate set map map++ GF Bart set 2288/s -- -84% -91% -92% -93% map 14566/s 537% -- -46% -48% -58% map++ 26756/s 1070% 84% -- -4% -23% GF 27881/s 1119% 91% 4% -- -19% Bart 34580/s 1412% 137% 29% 24% --

      See Re: Using map to create a hash/bag from an array for the original code

      DWIM is Perl's answer to Gödel

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://517462]
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree
[choroba]: Playing in a punk rock band for 20 years... my hearing is quite bad
[Corion]: I still have hopes to turn my godson and his two siblings into a punk band ;)
[Corion]: Their older sister just started piano but has been interested in drumming, which she should be able to start with 8 years or so)
[Discipulus]: ah was Kink of Bongo 1992..
[1nickt]: choroba name of band? youtube link? MySpace link?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-03-24 12:21 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.