Dave's Brain

Browse - programming tips - perl convert array to 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";
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: