You are recreating the functionality of grep.

Where you are using grep, that should be just a regexp or (even better) index. But back to the point, grep works on a list and returns a list of matches

#!/usr/bin/perl -w use strict; my @lines = ( 'this is line one', 'this is line TWO', 'this is line three', 'this is line four perl', 'this is line fiveperl', 'perl' ); my @search = grep { index($_,'perl')+1 } @lines; # my @search = grep {/perl/} @lines; # # if you don't want line five use /\bperl\b/; print "TRUE\n" if (@search); foreach (@search) { print "$_\n"; }

If you do use a $var to populate your regexp please use quotemeta

As for your other question 'if (@array)' will return true if there are any elements in the array (even if the only element is 0 or undef)

UPDATE: BTW here is the benchmark on the re vs. index solution

Benchmark: timing 100000 iterations of index, re... index: 4 wallclock secs ( 3.54 usr + 0.00 sys = 3.54 CPU) @ 28 +248.59/s (n=100000) re: 7 wallclock secs ( 6.98 usr + 0.00 sys = 6.98 CPU) @ 14 +326.65/s (n=100000)


grep
Mynd you, mønk bites Kan be pretti nasti...

In reply to Re: Checking Arrays by grep
in thread Checking Arrays by kidd

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.