[Israel.pm] array 2 hash for quick search
Gaal Yahas
gaal at forum2.org
Tue Jun 15 02:22:54 PDT 2004
On Tue, Jun 15, 2004 at 11:51:19AM +0300, Shlomo Yona wrote:
> 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;
> }
Do you care about hit counts? If not, this is pretty standard:
my %hash = map { $_ => 1 } @array;
If you do care, try this, which, uh, is what you did too:
my %hash; $hash{$_}++ for @array;
--
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/
More information about the Perl
mailing list