Trouble using <> operator  
Author Message
rcaruso





PostPosted: Visual FoxPro General, Trouble using <> operator Top

I'm comparing two character fields using the "<>" operator and I'm getting incorrect results if one of the fields happens to be empty.

Here is some code:

x = "abc"
y= " "

If alltrim(x) <> alltrim(y)
n = n + 1
endif

Since the IF statement evaluates to FALSE, n does not get incremented.

However, if I rewrite the IF statement as:

if not (alltrim(x) == alltrim(y))
n = n + 1
endif

the statement evaluates to TRUE, and n is incremented.

Anyone have any ideas why

RIC

VER: VFP 9.0 SP1




Visual FoxPro2  
 
 
Vedat ARAL





PostPosted: Visual FoxPro General, Trouble using <> operator Top

Try the first if statement with SET EXACT ON and SET EXACT OFF, you will see the difference.


 
 
AndyKr





PostPosted: Visual FoxPro General, Trouble using <> operator Top

>> if not (alltrim(x) == alltrim(y))
>> n = n + 1
>> endif
>> the statement evaluates to TRUE, and n is incremented.

>> Anyone have any ideas why

Maybe because the condition is TRUE!

If x = "abc" and y = " " then ALLTRIM(x) is not equal to ALLTRIM(y) and the for inequality is TRUE and so it should execute the loop. Remember, the part inside the parenthesis is executed first. So this statement comp[ares the values and then evaluates whether the result is true or false. Since you are testing for the NOT (i.e. statement is false) condition, the result is TRUE when the values are not identical.

The only time it will not execute, as you have it written, is if: ALLTRIM( x ) == ALLTRIM( y ), because then the statement is true and the NOT condifition is false!



 
 
rcaruso





PostPosted: Visual FoxPro General, Trouble using <> operator Top

This was "Exactly" the right answer. Many thanks!