Why does ne always work

G

Gary Mayor

Hi,
Whilst i'm here i might as well see if anyone else has noticed strange
things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
I always end up doing it a different way as i start pulling my hair out
because everything is correct it just doesn't work.

Anyone else had that problem.

Gary
 
G

Gary Mayor

Gary said:
Hi,
Whilst i'm here i might as well see if anyone else has noticed strange
things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
I always end up doing it a different way as i start pulling my hair out
because everything is correct it just doesn't work.

Anyone else had that problem.

Gary
 
A

Anno Siegel

Gary Mayor said:
Hi,
Whilst i'm here i might as well see if anyone else has noticed strange
things with if ($blah ne $blah) sometimes it works sometimes it doesn't.

Would you please explain what you mean by "doesn't work". "$blah ne
$blah" will always return false. It may also issue a warning if
$blah is undefined.
I always end up doing it a different way as i start pulling my hair out
because everything is correct it just doesn't work.

Anyone else had that problem.

What problem?

Anno
 
G

Gary Mayor

That's what i mean $blah ne $blah does not always return false. I must
say that $blah ne $blah1 is a better description I wish I still had some
of the code that gave me these errors but i have come accross it a
couple of times. Say i've got this,
$blah = "123";
$blah1 = "1234";

if ($blah ne $blah1) {
print "They are not equal";
}

I know this works but sometimes when the values are different it doesn't
always reconise they are not equal.

This message is probably useless without a real example. When I come
accross it again i'll post it up. I was just wondering if anyone else
had seen this problem.

Gary
 
A

Anno Siegel

[reply put into sequence. please don't top-post]

That's what i mean $blah ne $blah does not always return false. I must
say that $blah ne $blah1 is a better description I wish I still had some
of the code that gave me these errors but i have come accross it a
couple of times. Say i've got this,
$blah = "123";
$blah1 = "1234";

if ($blah ne $blah1) {
print "They are not equal";
}

I know this works but sometimes when the values are different it doesn't
always reconise they are not equal.

This message is probably useless without a real example. When I come
accross it again i'll post it up. I was just wondering if anyone else
had seen this problem.

I think you are confusing "ne" with "!=". If you compare two different
strings *numerically*, the result may be that they're equal because
they represent the same number, as in "003" and "3". String comparison
(with "eq", "ne" and friends) always compares strings "as-is".

There *may* be weird effects with overloaded objects, but that is
in another category. These effects have nothing to do with comparison,
but the strings you are comparing may not be the ones you think you
are comparing.

Anno
 
J

Jürgen Exner

[Would you please stop top-posting?]
[Would you please stop blindly full-quoting?]

Gary said:
That's what i mean $blah ne $blah does not always return false. I must
say that $blah ne $blah1 is a better description I wish I still had

Well, there is quite a difference between comparing the same variable and
comparing two different variables, don't you think? Would have been nice not
to misslead everyone :-(
some of the code that gave me these errors but i have come accross it
a couple of times. Say i've got this,
$blah = "123";
$blah1 = "1234";

if ($blah ne $blah1) {
print "They are not equal";
}

I know this works

Please define "works".
but sometimes when the values are different it
doesn't always reconise they are not equal.

Ah, finally a statement with some meat on it.
So you are claiming that the operator 'ne' yields the wrong result in some
circumstances. Is that correct?

Well, you know, may I quote from
http://www.catb.org/~esr/faqs/smart-questions.html:
<quote>
Q:. My program doesn't work. I think system facility X is broken.

A:. While it is possible that you are the first person to notice an
obvious deficiency in system calls and libraries heavily used by hundreds or
thousands of people, it is rather more likely that you are utterly clueless.
Extraordinary claims require extraordinary evidence; when you make a claim
like this one, you must back it up with clear and exhaustive documentation
of the failure case.

This message is probably useless without a real example.

I would agree!
When I come
accross it again i'll post it up. I was just wondering if anyone else
had seen this problem.

A wild guess:
In your example $blah and $blah1 are numbers, but you elected to denote and
compare them as strings.
If that is the style in which you usually write your programs, then I wonder
if maybe you ran into a 'perldoc -q 999' problem.

jue
 
G

Gary E. Ansok

I think you are confusing "ne" with "!=". If you compare two different
strings *numerically*, the result may be that they're equal because
they represent the same number, as in "003" and "3". String comparison
(with "eq", "ne" and friends) always compares strings "as-is".

Another possibility is that one of the strings has leading or
trailing whitespace. A common cause of this is forgetting to
chomp() a line read from a file, so that the string read from
the file still has the newline at the end, but the string it
is being compared to has no newline.

If I get strings that do not compare when I think they should,
my first step is to do something like

if ($blah ne $blah1) {
print "OOPS -- not equal '$blah' and '$blah1'\n";
}

Having the single quotes there helps to see any unexpected
spaces or newlines. You may prefer something more visible, like
">>>$blah<<<".

Yes, a real example would definitely help us help you.

Gary Ansok
 
T

Tintin

Gary Mayor said:
Hi,
Whilst i'm here i might as well see if anyone else has noticed strange
things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
I always end up doing it a different way as i start pulling my hair out
because everything is correct it just doesn't work.

Anyone else had that problem.

I'm going to take a stab in the dark here and suggest you are comparing
values from a file without chomping it.
 
A

Anno Siegel

Gary E. Ansok said:
Another possibility is that one of the strings has leading or
trailing whitespace.

There is only one string in "$blah ne $blah".

[snip]

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top