[mplspm]: In a sorted list [better]

Ken Williams ken at mathforum.org
Mon Mar 25 17:16:27 CST 2002


On Saturday, March 23, 2002, at 03:59 AM, John J. Trammell wrote:
> my $hash = sub
> {
>     my ($x,$a) = @_;
>     my %h = map { $_, 0 } @$a;
>     return exists $h{$x};
> };


Hello from Australia.

Since you only care about the keys and not the values, it's generally 
quicker to construct the hash like so:

my $hash = sub
{
     my ($x,$a) = @_;
     my %h;
     @h{@$a} = ();
     return exists $h{$x};
};

The real question is whether the original list would be better stored in 
a hash in the first place, avoiding that conversion each time.  The 
answer is usually yes.  In that case, it's *way* quicker.


  -Ken



--------------------------------------------------
Minneapolis Perl Mongers mailing list

To unsubscribe, send mail to majordomo at pm.org
with "unsubscribe mpls" in the body of the message.



More information about the Mpls-pm mailing list