jmclark has asked for the wisdom of the Perl Monks concerning the following question:
Can someone explain to me why the above code basically only prints every other time. For instance, the above only spits out:$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"; }
It seems to me as if the above should print:abc defgh 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:abc cde defgh gh fg
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!!$gw1, $gw2, $gw3, $gw4, $gw5 = $gw; if ($gw =~.... if ($gw1 =~.... if ($gw2 =~.... etc...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multiple if statements matching part of one variable problem
by GrandFather (Saint) on Sep 10, 2008 at 04:33 UTC | |
|
Re: Multiple if statements matching part of one variable problem
by pat_mc (Pilgrim) on Sep 10, 2008 at 08:49 UTC | |
by jmclark (Novice) on Sep 10, 2008 at 14:14 UTC | |
by ikegami (Patriarch) on Sep 10, 2008 at 14:20 UTC | |
by GrandFather (Saint) on Sep 10, 2008 at 14:35 UTC |