Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is "" equal to? Nothing?

Status
Not open for further replies.
Aug 1, 2003
39
US
In the following example the table of column converted.ndcnum is checked for a value. In one particular row of data in the table just this value is missing which causes the fputs not to write to the text file any further in the do while loop. lc_lastndc = ALLTRIM(converted.ndcnum) equal to ""

when the pgm returns to the

IF converted.status = "NEED" .and. ALLTRIM(converted.cfndc) <> lc_lastndc

the

converted.cfndc = "234578003" and lc_lastndc = ""

and it won't enter the if statement. No they are not equal but what keeps the if statement from running?

I am trying to understand why can't foxpro see that they are not equal. This is someone elses code and I am trying to figure what the person was trying to accomplish and what <b>""</b> means.

CODE:

local ln_FileHandle, ln_Position, ln_retval


ln_FileHandle = FCREATE(gc_convertarea + 'loadcf_' + TRIM(THISFORM.otb_shortname.value) +'.txt',0)
ln_Position = FSEEK(ln_FileHandle, 0) && Move the file pointer to BOF

*** OnDemand Header File Info
ln_RetVal = FPUTS( ln_FileHandle, "LOAD DATA" )
ln_RetVal = FPUTS( ln_FileHandle, "INFILE *" )
ln_RetVal = FPUTS( ln_FileHandle, "APPEND" )
ln_RetVal = FPUTS( ln_FileHandle, "INTO TABLE Item" )
ln_RetVal = FPUTS( ln_FileHandle, "FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '" + '"' + "'" )
ln_RetVal = FPUTS( ln_FileHandle, "TRAILING NULLCOLS" )
ln_RetVal = FPUTS( ln_FileHandle, "(Item,NDC,Name,Manufacturer)" )
ln_RetVal = FPUTS( ln_FileHandle, "BEGINDATA" )

LOCAL lc_lastndc
lc_lastndc = " "
SELECT converted
GOTO TOP
DO WHILE ! EOF()

IF converted.status = "NEED" .and. ALLTRIM(converted.cfndc) <> lc_lastndc
ln_RetVal = FPUTS( ln_FileHandle, ALLTRIM(converted.sitecode) + "," + ALLTRIM(converted.cfndc) + "," + ALLTRIM(converted.custdesc) + ",UNKNOWN" )
lc_lastndc = ALLTRIM(converted.ndcnum)
ENDIF

SELECT converted
SKIP
ENDDO

ln_retval = FCLOSE( ln_FileHandle )

A programmer is a red-eyed mumbling mammel capable of conversing with inanimate objects.
 

It looks like you need to check your SET EXACT setting (you need it ON) or, alternatively, code you IF statement like this:

IF converted.status=="NEED" .and. !ALLTRIM(converted.cfndc)==lc_lastndc

See if it solves it.
 

Could you clarify your question. Are you saying that ALLTRIM(converted.cfndc) <> lc_lastndc is true or is not true?

I would expect it to be not true if EXACT is set ON, and true if it is OFF.

Either way, the code is dangerous. If you want to force the test to fail, it would be better to put an "impossible" value in lc_lastndc, not a space.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 

You might also consider checking your value for EMPTYness:

IF converted.status=="NEED" .and. !EMPTY(ALLTRIM(converted.cfndc))
 
Thanks Stella and Mike for your help

I tried == and == Never entered the if end statement
"" == and <> Never entered the if end statement
"" = and <> Entered the if end statement
Set Exact is set to off on all of the runs.

It doesn't matter if the converted.cfndc is empty.

I will look into it more when I get a chance.

___________________________________

Thanks

Brian

A programmer is a red-eyed mumbling mammel capable of conversing with inanimate objects.
 

Set Exact is set to off on all of the runs.
Yes, exactly, that's why you might need == for your comparison, if you don't want to set to ON.

I tried == and == Never entered the if end statement
Did you notice/use the Exclamation Point, also?
Because you should try == and !==, not == and ==.

It doesn't matter if the converted.cfndc is empty.
What do you mean by "doesn't matter"? Do you mean that it didn't fix things or that you didn't try?
Checking if character string is EMPTY() in essence is about the same as comparison to "" with ==.

Also, do you use Debugger?
Are you sure your values are what you thing they are?

 

OK, Not sure but I think I might be onto somethink here.

You write every where that lc_lastndc = "", which means that EMPTY(lc_lastndc)=.T.

But in the code you posted it actually is not; lc_lastndc = " ", which means lc_lastndc==SPACE(1) (one space long).

If converted.cfndc = "234578003", even though not EMPTY() itself, it might come out as equal to something that is EMPTY(), depending on the EXACT setting. But no matter what EXACT is set to, "234578003" wouldn't come out as equal to SPACE(1).

Don't know if it would help you, but take a look at it.
 
Hello Brian.

I am trying to understand why can't foxpro see that they are not equal. This is someone elses code and I am trying to figure what the person was trying to accomplish and what "" means.

I see that the others here have given you the solution, but not the expalnation for what you are seeing. In case you are interested, here is the explanation (from a session I gave entitled "It Seemed Like a Good Idea at the Time":

When Comparing Character Strings Doesn’t!

[1] In Native VFP Code

At first glance comparing two strings is simple. After all, it is fundamental to all programming that we should be able to test two values and, depending on the result, take the appropriate action. When dealing with numeric data, logical fields and dates, there is little problem because the data is unambiguous. However, when we start trying to compare character data, things get a little more difficult. We have all written code like this:

IF lcCategory = ‘A’
*** DO this
ELSE
*** Do that
ENDIF

So how does Visual FoxPro decide whether the value which is pointed to by ‘lcCategory’ is actually equal to “A” or that lcChoice is equal to lcDefaultAction? The answer is, of course, that “it depends!”. The table below shows what happens when a simple comparison to a constant value is executed when SET( ‘EXACT’ ) = ‘OFF’:

Value Compare To Result
(Using “=”)
A A True
A a False
A “” True
a A False
a “” True

Well, the first two seem obvious enough, and so is the fourth, but what about number three and number five? What this seems to be telling us is that:

“A # a” and “a # A” but since both A = ‘’ AND a = ‘’ then logically “A = a”

After all it is axiomatic that two values which are equal to the same value must also be equal to each other! The answer to this apparent paradox lies in the way in which Visual FoxPro compares two strings. What is actually does is to take the string on the RIGHT of the equal sign and then compare the string on the LEFT character by character until the end of the string on the right is reached. If there have been no differences, the two strings are considered equal and the comparison returns TRUE. If there is nothing on the right of the comparison, there is nothing to compare the string on the left with. The result, because there has been no comparison at all is that there are no differences, so we get a return value of true. What it actually means is that no comparison was done at all!

So what happens if we swap the expression around so that the constant is on the left and our test value on the right? It now behaves exactly as we would expect.

Compare To Value Result
(Using “=”)
A A True
a A False
“” A False
A a False
“” a False

This behavior has puzzled many people over time, but it is actually quite logical when you remember that the strings are compared on the basis of length. So long as the string on the left matches the string on the right character by character the result is always true. “ABC” IS equal to “A” to Visual FoxPro. So when you are comparing a constant to a variable you can avoid the issue by placing the constant on the left of the expression. However, when comparing two variables you may not know whether one of them is actually an empty string and so cannot simply rely on having the expression ordered correctly!

If that were the end of the matter, it would be a very serious problem, fortunately it isn’t. There are two ways of resolving such comparisons properly. The first is to change the setting of “EXACT”. This setting, which is OFF by default, defines how Visual FoxPro compares two strings. By setting EXACT = ON we should be able to force Visual FoxPro to only accept a comparison as true when every character on the left hand side is matched exactly to the corresponding character on the right. However when we test it:

? “Does this match?” = “Does this match?” && TRUE
? “Does this match? ” = “Does this match?” && TRUE
? “Does this match?” = “Does this match? ” && TRUE

We still have a problem! This is because what SET EXACT actually does is to TRIM both expressions and compares the results. So we will now get return of true whenever the strings match except for trailing blanks! It seems to me that SET EXACT should really be named “SET ALMOSTEXACT”!
SET EXACT ON is not a good solution for two reasons. Firstly, because it is using a global setting to resolve a local problem. (Unless you actually test the current setting of EXACT every time you want to use a string comparison you can never be sure how Visual FoxPro will react!) Secondly, and this is the real killer, there are occasions when the trailing blanks can be important and using EXACT makes it impossible to include them.
The second, and much better solution, is to use the “==” (double equal, or ‘Exactly Equal’) operator. This tells Visual FoxPro that we really DO want to know if the true strings are exactly the same in all respects. Now, finally, we get the correct results in all situations.

? “Does this match?” == “Does this match?” && TRUE
? “Does this match? ” == “Does this match?” && FALSE
? “Does this match?” == “Does this match? ” && FALSE


Marcia G. Akins
 
Stella740pl said:
It looks like you need to check your SET EXACT setting (you need it ON) or, alternatively, code you IF statement like this:
IF converted.status=="NEED" .and. !ALLTRIM(converted.cfndc)==lc_lastndc
See if it solves it.

Using what Stella suggested this worked
!lc_lastndc == alltrim(converted.cfndc)
!alltrim(converted.cfndc)== lc_lastndc

MarciaAkins said:
So what happens if we swap the expression around so that the constant is on the left and our test value on the right? It now behaves exactly as we would expect.

Using what Marcia suggested (swapping it around) this worked..
lc_lastndc <> alltrim(converted.cfndc)

Here is what didn't work ( which is the original code)
alltrim(converted.cfndc) <> lc_lastndc
This didn't work because of what Marcia was stating. A non empty string when compared to an empty string on the right is true. But when I swapped it around it turns out false which is what is needed here.

Mike Lewis said:
Either way, the code is dangerous. If you want to force the test to fail, it would be better to put an "impossible" value in lc_lastndc, not a space.

Yes MikeLewis I agree putting no value in is a dangerous thing.

Thanks for all your help



--------------------------
Thanks
Brian

A programmer is a red-eyed mumbling mammel capable of conversing with inanimate objects.
 
Everyone's suggestions cover the 'equal to' question but noone has addressed your other question: 'and what <b>""</b> means.'. I am guessing that this code is writing an HTML document. I believe the <b> and </b> are HTML tags.

Once you get the IF statement to complete its work, look at the file created. You might find that it is an HTML
 
ross6720 this is an error on my part when learning the TGML tag stuff on tek-tips I put in <B>""</B> instead of [/B} so that when you see the post, the info inside appears bold like this "". The <B>""</B> is used in HTML code for Bold info between the markups.

Sorry for the confusion and Thanks for the info

--------------------------
Thanks
Brian

A programmer is a red-eyed mumbling mammel capable of conversing with inanimate objects.
 
ross6720 this is an error on my part when learning the TGML(tag markup language) on tek-tips I put in <B>""</B> instead of what was supposed to be used so that when you see the post, the info inside appears bold like this "". The <B>""</B> is used in HTML code for Bold info between the markups.

Sorry for the confusion and Thanks for the info

--------------------------
Thanks
Brian

A programmer is a red-eyed mumbling mammel capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top