This may be a little off the wall:

If all you're after is the instances of a particular number and you don't horribly care about the RE, you could try this:

sub count_instances { my $list = join ' ', shift(); my $val = shift; return $list =~ /\b$val\b/; }

It's quick and to the point but it does the very same RE slow-down that japhy warned about, but it doesn't use grep. This is still slower probably, but it will return the number of matches found and 0 if the number isn't there. If i got this wrong could someone please point out errors?

Update Sidenote:
If you just wanted to find out if the value is there, you could just use index($val, $list) which will return -1 if not found and the first index if it is found; assuming you use the string representation.


jynx

PS If you plan on passing an array to a function you should probably pass a reference, which makes the code more like:

sub count_instances { my $list = shift; my $val = shift; $list = join ' ', @{ $list }; return $list =~ /\b$val\b/; }


In reply to Re: better way to find list members? by jynx
in thread better way to find list members? by jptxs

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.