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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strong field comparison method 1

Status
Not open for further replies.

Bryan - Gendev

Programmer
Jan 9, 2011
408
AU
I want to cut out unneeded processing in a 15000 record table.
I am needing to check whether any one of five fields has been changed in my Import routine using a tab delimted txt file.

I import the file to an import dbf table and need to check against the original table. temp3

Simplistically I have

Code:
myntopic = Alltrim(Import.Topic)
myncaption = Alltrim(Import.Caption_1)
myndesc = Alltrim(Import.Descript_1)
mynref = Alltrim(Import.Reference)

myotopic = Alltrim(TEMP3.xname)
myocaption = Alltrim(TEMP3.Caption)
myodesc = Alltrim(TEMP3.Descript)
myoref = Alltrim(TEMP3.Reference)

I tried a comparison

Code:
If myntopic#myotopic Or;
  myncaption#myocaption Or;
  myndesc#myodesc  Or;
  myodesc#myndesc Or;
  mynref#myoref

  doit = .T.

endif

If doit = .t.

etc
etc

endif


if any of the myn variables don't match the myo variables I need to record the changes as doit = .t.

In some cases the comparison is against a myo variable of ''. The compariosn fails and doit stays at .f.

Is there a stronger test I can apply please?

GenDev





 
When comparing strings, VFP will compare the string on the right up to the string on the left. For example,

"SMITHE" will equal "SMITH" but "SMITH" will not equal "SMITHE". And, VFP will take into count trailing spaces on the right when doing the comparison so you get
"SMITH " will equal "SMITH" but "SMITH" will not equal "SMITH ".

What you need is "exactly equals". There are a couple of ways to do this. First, is SET EXACT ON before the IF statement. The default is OFF. I don't recommend this because it's easy SET ON and easy to forget to reset it so you run into issue in other places in the application

I think a better solution is to recode the IF statement using ==, which means exactly equals. Also, as a general rule, I always try to use a positive on tests rather than a negative (not) as it makes more sense to me. So, the IF statement will look like this:

IF myntopic == myotopic ;
AND myncaption == myocaption ;
AND myndesc == myodesc ;
AND myodesc == myndesc ;
AND mynref == myoref

DoIt = .T.

ELSE

DoIt = .F.

This will also have the advantage that the AND statement will run slightly faster than the OR statement because as soon as VFP finds one condition is false, it will stop processing the rest of them. With an or statement, VFP has to process every comparison.

Craig Berntson
MCSD, Visual C# MVP,
 
== is the stronger =, so far so good.
NOT (... == ...) is the stronger equivalent of (... # ...)

What craig also did is to turn all ORs to ANDs, this way in combination with == this checks for the case everything is excactly the same, and thus in this case you would need to set DoIt = .F., not .T.

Bye, Olaf.
 
Craig: a combination with all ORs also can stop at the first partly expression being .T.

Both If .T. Or Messagebox("Test")=0 and also If .F. AND Messagebox("Test")=0 doen't execute the Messagbox, as the boolean logic is optimized.

In both cases the expression evaluation stops at the first field differing, there is no advantage of AND to OR.

Bye, Olaf.

 
Thanks Guys,

I appreciate your explanations in trying to help me.

I'm in a situation with a user that if my app misses even one 'new' entry he'll be on my back.

So I await with interest a FINAL resolution of what I would best put in my code.


GenDev
 
Well, you got the final resolution already. All you need to apply is either "craig... checks for the case everything is excactly the same, and thus in this case you would need to set DoIt = .F., not .T." What does that mean, gendev? You just need to inverse how craig sets doit. Or you apply what I said: NOT (... == ...) is the stronger equivalent of (... # ...). So instead of # you put NOT ==, in the syntax NOT (a==b).

You can choose between

Code:
IF myntopic == myotopic ;
AND myncaption == myocaption ;
AND myndesc == myodesc ;
AND mynref == myoref 

DoIt = .F.

ELSE

DoIt = .T.
ENDIF

or

Code:
IF NOT (myntopic == myotopic) ;
OR NOT (myncaption == myocaption) ;
OR NOT (myndesc == myodesc) ;
OR NOT (mynref == myoref) 

DoIt = .T.

ELSE

DoIt = .F.
ENDIF

Craigs expression wasn't wrong, but how he set DoIt, was inverse to what you need. And I only added, that it doesn't matter for optimization reasons, if you AND partial expressions or OR other partial expressions.
Do you understand this now?

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top