![]() |
|
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?? |
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:
— 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 Section
Seekers of Perl Wisdom
|
|