To find if a word $a is in a string $b you can use a regex to match a word boundary, the word you are looking for and a word boundary: $b =~ /\b$a\b/;.

You can have Perl build such a regex using the words of your array by joining the words with the pipe symbol to seperate the alternatives. (You will have to group this part of the regex to require the word boundaries on either side of a word.)

Using the regex Perl built for you, you can grep through the strings in @B to find the number of times there's a match, leading to:

$words = join '|' => @A; if (grep /\b(?:$words)\b/, @B) { # do this } else { # do that }

— Arien

Edit: To just find out if there is a match, it is faster to use a loop and break out of it (after setting a flag) when you find a match like fruiture does.


In reply to Re: Simple for some. by Arien
in thread Compare two lists of words by nasa

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.