Hm, not sure of what you are trying to do, but assuming I understood, one possibility might be using the smart match:
DB<38> use v5.10.0; DB<39> $var = 7; DB<40> @a = qw/ 4 6 7 9 3/; DB<41> print "yes" if $var ~~ @a; yes
(I know it has been deprecated and I would certainly not use that for production code that is supposed to last, but there are cases where you might want to use it nonetheless because it is a short-life project, for example.)

grep is an alternative:

print "yes" if grep { $var == $_} @a;
Or you could use the any function of List::Util module (if your version of that module is recent enough, version 1.33 or above, I think), with just about the same syntax as the grep line above. This should often be faster than grep because any will short-circuit as soon as it has found a matching element, and grep won't.

If your version of List::Util is too old, then try any in List::MoreUtils, any has been around with it for longer. Or you could implement it yourself with List::Util's reduce function (the reduce documentation shows basically how to do it).

With reduce, you could even implement an iterator-based lazy version of grep that will short-circuit for you (but will usually not be faster, except when the list is long and the item found early in the list).

Or you might turn to Perl 6. ;-)

But all the above might just be off-topic if I misunderstood your requirement. Please explain further.


In reply to Re: Create a new operator, get LHS by Laurent_R
in thread Create a new operator, get LHS by stevieb

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.