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

Andy Lester andy at petdance.com
Wed Sep 14 11:55:46 PDT 2005


On Wed, Sep 14, 2005 at 09:46:28AM -0500, Jay Hannah (jhannah at 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 at petdance.com => www.petdance.com => AIM:petdance


More information about the Omaha-pm mailing list