Re: BInding operator fails
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 26 Feb 2007 09:31:27 -0800
On Feb 26, 12:09 pm, lovepe...@xxxxxxx wrote:
Hi. I have a problem with the below code. I have two strings, $rdns and $result1. I
want to make sure $result 1 is NOT part of $rdns. But the below fails...thus instead
of printing the else part of the if-else-loop. It print the main part. Does anyone
know what coudl cause this.
Yes. You making mistakes. Please never assume the language is to
blame. It's far more likely to be programmer error.
$rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,ou=InJoin,ou=applications,dc=marriott,dc=com
pwdChangedTime=20070101120000.000000Z";
$result1="Exchange";
if ($result !~ /$rdns/ix) {
print "\nresult: '$result'";
print "\nrdn: '$rdns'\n";
} else {
print "it is not there\n";
}
You have two separate and distinct problems, both with your logic.
First of all, you're looking for $rdns inside of $result. Your
description and example strings suggest you want it the other way
around. Secondly, you're using the negative binding operator, which
returns true if the pattern is *not* found, but the statements in the
if-else block are backwards.
if ($rdns !~ /$result/ix) {
print "'$rdns' does not contain '$result'\n";
} else {
print "'$rdns' DOES contain '$result'\n";
}
You also have two other, more minor problems.
For one, you're using the x modifier, which means that Perl isn't
looking for any of the whitespace in your pattern. Secondly, you're
blindly putting a variable into the pattern match, without taking into
account whether or not any of the characters in that variable are
special to a regular expression. You should instead have:
if ($rdns !~ /\Q$result\E/i)
And your final problem is that you're using regular expressions when
you have no reason to be using regular expressions. $result isn't a
pattern, it's just a string. If you just want to see if one string is
contained inside another, you use the index function:
if (index($rdns, $result) == -1) {
print "'$result' not found in '$rdns'\n";
}
Hope this helps,
Paul Lalli
.
- References:
- array operation
- From: Irfan Sayed
- Re: array operation
- From: John W. Krahn
- BInding operator fails
- From: loveperl6
- array operation
- Prev by Date: Re: BInding operator fails
- Next by Date: Re: BInding operator fails
- Previous by thread: Re: BInding operator fails
- Next by thread: Re: array operation
- Index(es):
Relevant Pages
|