[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] map: make a hash from an array



On Wed, Sep 14, 2005 at 09:46:28AM -0500, Jay Hannah (jhannah@omnihotels.com) wrote:
> Here's a faster way:
> 
> my %keepthese = map { $_, 1 } @keepthese;

If you only care about the existence of a given element, and don't care
if it gets a value, you can assign to a hash slice:

    my %hash;
    @hash{@keepthese} = ();

If you need them to have a value, you can do this:

    my %hash;
    @hash{@keepthese} = (1) x @keepthese;

Finally, if you're just checking for existence, and don't really need
to worry about speed, you can do

    my $exists = grep { $_ eq $searching_for }, @keepthese;

xoxo,
Andy

-- 
Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance