Hi all,
I have a large array of strings and I would like to pull all strings from the array that contain multiple substrings.
I've tried the following which does not work:
$largeArray[0] = "this is a test";
$largeArray[1] = "this is another test";
$largeArray[2] = "that is another test";
$largeArray[3] = "test this is another";
$searchCriteria[0] = "another";
$searchCriteria[1] = "test";
if (@newArray = grep {/@searchCriteria/} @largeArray) {
print @newArray;
}
This returns (incorrect):
this is another test
that is another test
So using the same @largeArray, I tried the following:
if (@newArray = grep {/another/ and /test/} @largeArray) {
print @newArray;
}
which returns (correct):
this is another test
that is another test
test this is another
My problem is that I cannot be certain how many elements will be in my @searchCriteria. Therefore it is not feasable to put a grep together in the latter form.
Any ideas or suggestions (or am I missing something fundamental)?
Thanks,
Brian
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.