Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Check Array Elementsby davido (Cardinal) |
on Jun 06, 2012 at 22:09 UTC ( [id://974842]=note: print w/replies, xml ) | Need Help?? |
If it's just a one-time check, or if the array changes frequently, it's probably not all that efficient to suck the whole thing into a hash just to do three exists tests. Converting the big set to a hash is an O(n) operation, but a computationally more expensive one than the O(n) operation of grepping for the thee elements.
This method uses a small three-element hash as the lookup list. Each time an entry is found, it is deleted from the lookup hash so that you assure that all three items are found (as opposed to the same item three times). If the big array changes infrequently, and you will be doing more than a few lookups on it, then it makes sense to convert the big array to a hash and invert the search:
Dave
In Section
Seekers of Perl Wisdom
|
|