[Israel.pm] array 2 hash for quick search

David Baird david.baird at homemail.com
Tue Jun 15 02:02:25 PDT 2004


> Is there any other idiom for transforming an array to a hash
> for quick search? I'm looking for some short hack.
> 
> sub array2hash {
> 	my ($array) = @_;
> 	my %hash;
> 	foreach my $array_item (@$array) {
> 		$hash{$array_item}++
> 	}
> 	return \%hash;
> }
> 

What about:

%hash = map { $_, 1 } @array;

-David




More information about the Perl mailing list