Array to hash

 | Perl | 2 Comments

Ever wondered how to quickly convert an array to a hash on the fly? Well here's one way to do it:

my %h = map { $_ => 1 } qw/one two three four/;

Feel free to come up with other perhaps more efficient and/or elegant and/or cryptic ways of doing this.

2 Comments

my %h; @h{qw/one two three four/} = undef;

my %h = map { $_ => 1 } qw/one two three four/; # 47 characters

my %h; @h{qw/one two three four/} = undef; # 44 characters

You win by a nose!

Leave a comment

Recent Entries

A walk along the Keizersgracht
Too often one is so consumed by a jungle of intertwined thoughts that the beauty of the nearby surroundings ... »
Popularity is fickle
The popularity of a given next generation technology is very fickle, and its success or failure depends on many ... »
Where was Kiffin really buried?
Hi There seems to be some confusion on the current resting place of Kiffin Rockwell, some say that his ... »
Going to Portugal
Normally the week just before I leave for summer vacation, I spend hours on end desperately searching for some ... »
A human language
These days it is not very often that a new and exciting Perl book comes along. That's why I ... »