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:

if( grep { $_ eq $test } ( $item1, $item2 ) ) { #..... This was an "or" } if( grep { $_ eq $test } ( $item1, $item2 ) == 2 ) { #..... This was an "and" }

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:

use List::MoreUtils qw/ any all /; #Here is an "or" condition. if( any { $_ == 1 } ( 1, 2, 3 ) ) { print "True.\n"; # This one is true because one of the elements matched. } #Here is an "and" condition. if( all { $_ == 1 } ( 1, 2, 3 ) ) { print "True.\n"; # This one is NOT true, because only one of the three elements mat +ched. }

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:

use Quantum::Superpositions; if( any( 1, 2, 3 ) == 1 ) { print "True.\n"; } if( all( 1, 2, 3 ) == 1 ) { print "True.\n"; # This one fails because only one, not all elements matched. }

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 reply to Re: testing more than one item for equality by davido
in thread testing more than one item for equality by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.