Syntactic Confectionery Delight | |
PerlMonks |
Re: testing more than one item for equalityby davido (Cardinal) |
on Oct 18, 2006 at 15:52 UTC ( [id://579121]=note: print w/replies, xml ) | Need Help?? |
I actually kind of like that grep solution because it allows you to implement both "this or that", or "this and that". Here's how:
This works because grep in scalar context returns the number of times it matched. If it matched an equal number of times to the number of elements being tested, all elements matched, so you've got a "this and that and that and that" condition. This type of test is simplified even more (and definitely made easier to read) by using List::MoreUtils. That module offers an any and an all function:
The List::MoreUtils solution is sort of like the grep solution, but IMHO, easier to "at a glance" understand what's going on, and thus a better choice for its clarity. Quantum::Superpositions could be used like this:
Though the Quantum::Superpositions solution looked the simplest, the module's own author (TheDamian) states that it is not intended for real world use. It was written more as a proof of concept, so while it's fun to play with, you probably shouldn't put it in production code. My own experience is that it's stable, but often slow, probably because it's such a brute force generalized approach to problems that may have more efficient specific solutions. Dave
In Section
Seekers of Perl Wisdom
|
|