in reply to Re: better way to find list members?
in thread better way to find list members?

foreach (@animals) { $animals{$_}++; } # initialize %animals hash
Here's a faster way to do that (looks neat too):
@animals{@animals} = (1) x @animals;
It's not exactly the same since it's setting each hash member to 1 instead of ++'ing it, but that's no big deal if you're just checking for an element's existance.

-Matt

Replies are listed 'Best First'.
RE: RE: Re: better way to find list members?
by runrig (Abbot) on Oct 18, 2000 at 03:58 UTC
    If all you want is existence, then this:
    @animals{@animals}=undef; print "Got a dog!\n" if exists $animals{dog};
    is faster and uses less memory.
RE: RE: Re: better way to find list members?
by myocom (Deacon) on Oct 18, 2000 at 03:38 UTC

    Are you sure you meant to do @animals{@animals}? That code seems fraught with peril...

    Update: Actually, the more I look at it, the more it's starting to possibly make some sense to me...