Browse - Programming Tips - How can I quickly convert an array to a hash?

Date: 2011aug25
Language: perl

Q.  How can I quickly convert an array to a hash?

A.  Use grep like this:

my @a = qw(one two three four); my %h; grep { $h{$_} = 1 } @a; # Convert to hash # Now its easy to see if something is a in the array if (exists $h{three}) { print "Yes, three is present\n"; }