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

Re: 1 line array to hash converter

by stefp (Vicar)
on Sep 21, 2001 at 18:39 UTC ( [id://113881]=note: print w/replies, xml ) Need Help??


in reply to 1 line array to hash converter

$hash{$_}++ for @array;

The value associated to a key is the number of elements in @array which have the value of the said key.

The solution you searched for and explicited by suaveant gives 1 for each value even if the key appears many times in the array.

TMTOWTDI.

-- stefp

Replies are listed 'Best First'.
Re: Re: 1 line array to hash converter
by rossta (Initiate) on Sep 21, 2001 at 18:55 UTC
    Thanks everyone. I think stefp offered the most elegant solution, because: 1. Shortest amount of code. 2. Elegant and easy to read and understand. 3. Assigns something useful to the hash value. Of course, in hindsight, it was *obvious*.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2025-05-21 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.