Monks, I've boiled my problem down to the simplest following code:
$gw="abcdefgh"; if ($gw =~ m/abc/ig){ print "abc\n"; } if ($gw =~ m/cde/ig){ print "cde\n"; } if ($gw =~ m/defgh/ig){ print "defgh\n"; } if ($gw =~ m/gh/ig){ print "gh\n"; } if ($gw =~ m/fg/ig){ print "fg\n"; }
Can someone explain to me why the above code basically only prints every other time. For instance, the above only spits out:
abc defgh fg
It seems to me as if the above should print:
abc cde defgh gh fg
Is there something I'm missing? I've got a script that checks the name of device and based on parts of its name I then know the devices function and need to run other things against that device. And it is completely possible that anywhere from 1 -5 of the if statements could match the device name and need additional code run in my script. The only work around I've come up with at this point is to basically say:
$gw1, $gw2, $gw3, $gw4, $gw5 = $gw; if ($gw =~.... if ($gw1 =~.... if ($gw2 =~.... etc...
It also doesn't matter if you change the order of the if statements, it will always match the 1st, skip the 2nd, match the 3rd, skip the 4th, etc. Thanks and I look forward again to the wisdom!!

In reply to Multiple if statements matching part of one variable problem by jmclark

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.