Dave's Brain

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"; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.