Greetings, monks.
I have come in search of wisdom that I have not found anywhere else; neither the the Camel book nor Googling has come up with an answer to my question and I hope that it may be answered here.
I am trying to find multiple words in a sentence, and if I find them, act accordingly. To wit, I wrote:
if ($sentence =~ /foo/gis && $sentence =~ /bar/gis)
{
# do something further with $sentence
}
But it is wrong, the second expression is always false, even if the sentence does contain a "bar".
I rewrote it to say:
if ($sentence =~ /foo/gis)
{
if ($sentence =~ /bar/gis)
{
# now do something with the sentence
}
}
but that doesn't work either! The only way I could get it to work was:
if ($sentence =~ /foo/gis)
{
my $final_test = $sentence;
if ($final_test =~ /bar/gis)
{
# *now* do something with the sentence...
}
}
I apologize if this is a real "duh" question; it seemed like it would make sense to me (a C++ programmer) but the second try really confused me as I figured it surely couldn't fail, it was just doing another search.
I beg wisdom and mercy for my cluelessness.
Thank you,
Tacho
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.