Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Simple for some.

by Arien (Pilgrim)
on Aug 18, 2002 at 13:32 UTC ( [id://190981]=note: print w/replies, xml ) Need Help??


in reply to Compare two lists of words

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://190981]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2025-02-09 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.