Home > Archive > PERL Beginners > February 2007 > BInding operator fails
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
BInding operator fails
|
|
| loveperl6@aol.com 2007-02-26, 7:00 pm |
| All,
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 anyo
ne know what coudl cause this.
$rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,ou=InJoin,ou=appli
cations,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";
}
Thanks - Bill
________________________________________
________________________________
Check out the new AOL. Most comprehensive set of free safety and security tools, free access to millions of high-quality videos from across the web, free AOL Mail and more.
| |
| Adriano Ferreira 2007-02-26, 7:00 pm |
| On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> wrote:
> All,
>
> 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 an
yone know what coudl cause this.
>
> $rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,ou=InJoin,ou=appli
cations,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";
> }
>
I think that you want to say that instead:
if ($rdns !~ /\Q$result1\E/ix) {
to make sure you can't find the contents of $result1 inside $rdns. You
may use the Perl built-in index as well, which will be even faster.
And don't forget to run your code under
use strict;
use warnings;
because your piece of code assigns to $result1 and test $result. That
would be obvious with strict and warnings.
| |
| Paul Lalli 2007-02-26, 7:00 pm |
| On Feb 26, 12:09 pm, lovepe...@aol.com 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=appli
cations,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
| |
| Tom Phoenix 2007-02-26, 7:00 pm |
| On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> 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.
> if ($result !~ /$rdns/ix) {
That's checking whether $rdns, as a pattern, does not match the string
in $result. (Was that supposed to be $result1?) But I think you're
asking for this, maybe:
if (index($rdns, $result) == -1) {
print "\$result isn't part of \$rdns.\n";
}
The index function is covered in perlfunc. Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| loveperl6@aol.com 2007-02-26, 7:00 pm |
|
Thank you both. I am trying to find out why "!~" operator fails. It is due to the whitespaces. but I am using "six" to ignore spaces.
Sorry guys. Below is the actual code. I made the changes that A.R. Ferreira suggested and it fails.
use strict;
use warnings;
my $rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,o
u=InJoin,ou=applications,dc=marriott,dc=
com";
my $result="cn=Exchange Sites";
if ($result !~ /\Q$rdns\E/six) {
print "\nresult: '$result'";
print "\nrdn: '$rdns'\n";
} else {
print "String is there\n";
}
OUTPUT is:
$ ./test.pl
result: 'cn=Exchange Sites'
rdn: 'cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,ou=InJoin,ou=appli
cations,dc=marriott,dc=com'
Tom your code works fine. But I was tring to understand why "!~" fails above.
use strict;
use warnings;
my $rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,o
u=InJoin,ou=applications,dc=marriott,dc=
com";
my $result="cn=Exchange Sites";
if ($result !~ /\Q$rdns\E/six) {
print "\nresult: '$result'";
print "\nrdn: '$rdns'\n";
} else {
print "String is there\n";
}
OUTPUT is:
String is there
-----Original Message-----
From: tom@stonehenge.com
To: loveperl6@aol.com
Cc: beginners@perl.org
Sent: Mon, 26 Feb 2007 12:29 PM
Subject: Re: BInding operator fails
On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> 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.
> if ($result !~ /$rdns/ix) {
That's checking whether $rdns, as a pattern, does not match the string
in $result. (Was that supposed to be $result1?) But I think you're
asking for this, maybe:
if (index($rdns, $result) == -1) {
print "\$result isn't part of \$rdns.\n";
}
The index function is covered in perlfunc. Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
-- To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
________________________________________
________________________________
Check out the new AOL. Most comprehensive set of free safety and security tools, free access to millions of high-quality videos from across the web, free AOL Mail and more.
| |
| Paul Lalli 2007-02-26, 7:00 pm |
| On Feb 26, 12:49 pm, lovepe...@aol.com wrote:
> Thank you both. I am trying to find out why "!~" operator fails.
It doesn't. You do. You completely ignored everything in my
article. Go read it again.
Paul Lalli
| |
| Adriano Ferreira 2007-02-26, 7:00 pm |
| On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> wrote:
>
> Thank you both. I am trying to find out why "!~" operator fails. It is due to the whitespaces. but I am using "six" to ignore spaces.
It has nothing to do with whitespaces. As Tom said:
On 2/26/07, Tom Phoenix <tom@stonehenge.com> wrote:
>
> That's checking whether $rdns, as a pattern, does not match the string
> in $result. (Was that supposed to be $result1?) But I think you're
> asking for this, maybe:
To check that $result1 is not a part of $rdns, you must say
$rdns !~ /\Q$result1\E/ix
# which reads as:
# this string does not match the literal content
of $result1
If you exchange the pieces, it will always fail to find that the
smaller $result1 has a large piece $rdns within it.
> Sorry guys. Below is the actual code. I made the changes that A.R. Ferreira suggested and it fails.
>
> use strict;
> use warnings;
>
> my $rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,o
> u=InJoin,ou=applications,dc=marriott,dc=
com";
>
> my $result="cn=Exchange Sites";
>
> if ($result !~ /\Q$rdns\E/six) {
> print "\nresult: '$result'";
> print "\nrdn: '$rdns'\n";
> } else {
> print "String is there\n";
> }
> OUTPUT is:
> $ ./test.pl
> result: 'cn=Exchange Sites'
> rdn: 'cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,ou=InJoin,ou=appli
cations,dc=marriott,dc=com'
>
> Tom your code works fine. But I was tring to understand why "!~" fails above.
>
> use strict;
> use warnings;
>
> my $rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine Configuration,ou=Conf,o
> u=InJoin,ou=applications,dc=marriott,dc=
com";
>
> my $result="cn=Exchange Sites";
>
> if ($result !~ /\Q$rdns\E/six) {
> print "\nresult: '$result'";
> print "\nrdn: '$rdns'\n";
> } else {
> print "String is there\n";
> }
> OUTPUT is:
>
> String is there
>
>
>
> -----Original Message-----
> From: tom@stonehenge.com
> To: loveperl6@aol.com
> Cc: beginners@perl.org
> Sent: Mon, 26 Feb 2007 12:29 PM
> Subject: Re: BInding operator fails
>
>
> On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> wrote:
>
>
>
> That's checking whether $rdns, as a pattern, does not match the string
> in $result. (Was that supposed to be $result1?) But I think you're
> asking for this, maybe:
>
> if (index($rdns, $result) == -1) {
> print "\$result isn't part of \$rdns.\n";
> }
>
> The index function is covered in perlfunc. Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training
>
> -- To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
> ________________________________________
________________________________
> Check out the new AOL. Most comprehensive set of free safety and security tools, free access to millions of high-quality videos from across the web, free AOL Mail and more.
>
| |
| Tom Phoenix 2007-02-26, 7:00 pm |
| On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> wrote:
> Tom your code works fine. But I was tring to understand why "!~" fails
> above.
>
> That's checking whether $rdns, as a pattern, does not match the string
> in $result.
That's why it "fails": It does not mean what you think it means.
It means, use $rdns as a pattern, to try to match the string in
$result. Then it returns false if the match succeeded or true if the
match failed (just the boolean opposite of what =~ would have done).
Here's another way to write it:
if (not ($result =~ /$rdns/ix) ) {
But you originally said "I want to make sure $result is NOT part of
$rdns." That's not the same as making sure that $rdns is not part of
$result.
Does this clear things up?
Cheers!
--Tom Phoenix
Stonehenge Perl Training
| |
| D. Bolliger 2007-02-26, 7:00 pm |
| loveperl6@aol.com am Montag, 26. Februar 2007 18:49:
[snip]
Hi!
> use strict;
> use warnings;
>
> my $rdns="cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine
> Configuration,ou=Conf,o u=InJoin,ou=applications,dc=marriott,dc=
com";
>
> my $result="cn=Exchange Sites";
>
> if ($result !~ /\Q$rdns\E/six) {
> print "\nresult: '$result'";
> print "\nrdn: '$rdns'\n";
> } else {
> print "String is there\n";
> }
> OUTPUT is:
> $ ./test.pl
> result: 'cn=Exchange Sites'
> rdn: 'cn=Exchange Sites,cn=Proxy Views,cn=JoinEngine
> Configuration,ou=Conf,ou=InJoin,ou=appli
cations,dc=marriott,dc=com'
>
> Tom your code works fine. But I was tring to understand why "!~" fails
> above.
[snip]
The regex expression tries to match $rdns in $result, and not the reverse way,
and of course you can't find a long string in a shorter one, even not if the
shorter one is contained in the longer one.
So $rdns is never matched in $result, and since you ask if it does *not*
match, the result is always true, and the else branch is never hit.
Dani
P.S.:
Please open a new thread when asking a new question, and answer inline or at
the bottom, not a the top, thx :-)
| |
| loveperl6@aol.com 2007-02-26, 7:00 pm |
| Tom/A.R.,
I owe you both an appology. You guys are correct, my origional statement is different than what I was trying to say.
Thank you both so much.
Bill
-----Original Message-----
From: tom@stonehenge.com
To: loveperl6@aol.com
Cc: beginners@perl.org
Sent: Mon, 26 Feb 2007 1:10 PM
Subject: Re: BInding operator fails
On 2/26/07, loveperl6@aol.com <loveperl6@aol.com> wrote:
> Tom your code works fine. But I was tring to understand why "!~" fails
> above.
>
> That's checking whether $rdns, as a pattern, does not match the string
> in $result.
That's why it "fails": It does not mean what you think it means.
It means, use $rdns as a pattern, to try to match the string in
$result. Then it returns false if the match succeeded or true if the
match failed (just the boolean opposite of what =~ would have done).
Here's another way to write it:
if (not ($result =~ /$rdns/ix) ) {
But you originally said "I want to make sure $result is NOT part of
$rdns." That's not the same as making sure that $rdns is not part of
$result.
Does this clear things up?
Cheers!
--Tom Phoenix
Stonehenge Perl Training
-- To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
________________________________________
________________________________
Check out the new AOL. Most comprehensive set of free safety and security tools, free access to millions of high-quality videos from across the web, free AOL Mail and more.
|
|
|
|
|