Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: better array to hash conversion

by Anonymous Monk
on Dec 11, 2012 at 12:23 UTC ( [id://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;

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found