Re: BInding operator fails



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

.



Relevant Pages

  • Re: what is the quickest way to find out whether a string contains another string?
    ... Why would anyone want to use regular expressions to look for a plain ... Regular expressions are a tool for deciding whether a String or ... searching is a trivial degeneration of the problem addressed by regular ... input string into a pattern that matches only itself is the first task. ...
    (comp.lang.java.programmer)
  • Re: ACT scripting
    ... While executing the the script send by you..I am getting the following ... > 1) Assuming the pattern you are looking for is unique in the file, ... > 'string to search ... > Regular Expressions. ...
    (microsoft.public.scripting.vbscript)
  • Re: Pattern Match
    ... Is there any way to get them in one pattern? ... Here's a demo program I use to test out regular expressions. ... quotes if the expression has spaces) and a string to test (again ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: RegExp irregularity in JScript
    ... using Regular Expressions to do it. ... takes a pattern and an string, and tells us whether the string matches the ... pattern when the user clicks a button. ... But - when we validate some strings using this code, ...
    (microsoft.public.scripting.jscript)
  • Re: BInding operator fails
    ... That's checking whether $rdns, as a pattern, does not match the string ... (Was that supposed to be $result1?) ...
    (perl.beginners)