Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: better array to hash conversion

by Anonymous Monk
on Dec 11, 2012 at 12:23 UTC ( #1008289=note: print w/ replies, xml ) Need Help??


in reply to better array to hash conversion

The following code does this, but I wonder if there is a more elegant and/or efficient way of doing this?

Really? What search terms did you use to look?

Did you benchmark?

 my %hash; @hash{ @array } = 0 .. $#array;


Comment on Re: better array to hash conversion
Download Code
Re^2: better array to hash conversion
by perltux (Beadle) on Dec 11, 2012 at 12:35 UTC
    I did do searches for "array to hash conversion" but all results came up with examples that use 'map' and did not seem to work for my specific conversion (the array values becoming the keys and the array index numbers becoming the values).

    Sorry for asking what might look like a dumb question to an expert, but my Perl skills are still limited.

    Anyway, thanks for your reply and thanks to everybody else who replied, too!

      Sorry for asking what might look like a dumb question to an expert, but my Perl skills are still limited.

      :) My intent was not to admonish, I was interested to know your search terms (you should share them if you searched) as this topic can be hard to find.

      I tried a bunch variations, I even resorted to ?node_id=3989;BIT=benchmark%20hash%7B, closest I found was Array to Hash, Using map to create a hash/bag from an array -- not your case exactly, but seems to cover all available syntax -- you could adapt it

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-07-13 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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








    Results (251 votes), past polls