Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: better array to hash conversion

by karlgoethebier (Curate)
on Dec 11, 2012 at 12:28 UTC ( #1008291=note: print w/ replies, xml ) Need Help??


in reply to better array to hash conversion

Like this..

use Data::Dumper; my @array = (a..z); my %hash = map { $array[$_] => $_ } 0..$#array; print Dumper(\%hash); __END__ $VAR1 = { 'w' => 22, 'r' => 17, 'a' => 0, 'x' => 23, 'd' => 3, 'j' => 9, 'y' => 24, 'u' => 20, 'k' => 10, 'h' => 7, 'g' => 6, 'f' => 5, 't' => 19, 'i' => 8, 'e' => 4, 'n' => 13, 'v' => 21, 'm' => 12, 's' => 18, 'l' => 11, 'c' => 2, 'p' => 15, 'q' => 16, 'b' => 1, 'z' => 25, 'o' => 14 };

Update...i cheated this one ;-)

@hash{@array} = 0..$#array;
map

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»


Comment on Re: better array to hash conversion
Select or Download Code
Re^2: better array to hash conversion
by perltux (Beadle) on Dec 11, 2012 at 13:31 UTC
    Actually I do like your example with 'map' because it made me understand better how 'map' works (even though @hash{@array} = 0..$#array; is probably the better solution here).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-07-11 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls