I tend to agree with your hesitation Hagbone. I try to avoid using hashes merely for looking up keys and throwing away the values, but that's mostly a religious perspective on my part, and in many cases that's the cleanest way to code a lookup function. A couple of observations: first, in this situation it is common to initialize the hash using a hash slice where your values are '1'. So,
my %lookup_hash = (); %lookup_hash{@whatever} = (1) x @whatever;
Secondly, a hash lookup is much faster than iterating through an array as the number of values increases. So, the "waste" of the values in a hash to gain lookup speed is sometimes a worthwhile tradeoff.
Note that in your second example (if (/$hash{$whatever}/) ) you needn't do a match against the value returned from your hash; rather, merely check the key like so: if ($hash{$whatever}).
In reply to Re: Creating lists using a hash with the same key/values
by djantzen
in thread Creating lists using a hash with the same key/values
by Hagbone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |