Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: better array to hash conversion

by karlgoethebier (Abbot)
on Dec 11, 2012 at 12:28 UTC ( [id://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»

Replies are listed 'Best First'.
Re^2: better array to hash conversion
by perltux (Monk) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1008291]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found