Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: mapping two arrays into a hash

by integral (Hermit)
on Feb 26, 2003 at 21:12 UTC ( #238919=note: print w/ replies, xml ) Need Help??


in reply to mapping two arrays into a hash

Zigdon's answer shows the very simple (and good) way to do it, but as you ask you can do this using map to interleave the two arrays (this might be useful for other data structures too).

my (%hash, @a, @b); %hash = map { ($a[$_], $b[$_]) } 0..$#a;
Update: This is the same as tachyon's code.

Although as always TMTOWTDI applies, and you can do it with other loops, although the one below is destructive:

my (%hash, @a, @b); $hash{shift @a} = shift @b while (@a and @b);

--
integral, resident of freenode's #perl


Comment on Re: mapping two arrays into a hash
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-07-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (198 votes), past polls