The set is small? And constant? Forget the hash, you have fallen into an XY trap.
If they contain only alphabetical characters (or some subset) then it is a trivial matter to come up with a character that is unused by any of the set members.
Build a string by joining the values using the delimiter character, and bracket the beginning and end of the string. You can then use index to determine membership. If the set is small enough, this will be faster than any hash or regexp solution.
my $delim = ","; my $set = $delim . join($delim, keys %set) . $delim; sub IsMember { return index($set, "$delim$_[0]$delim") != -1; }
• another intruder with the mooring in the heart of the Perl
In reply to Re: Fastest way to test membership in a constant set (don't use a hash)
by grinder
in thread Fastest way to test membership in a constant set
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |