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

foxpro IF statement with too many ORs 3

Status
Not open for further replies.

linousa

IS-IT--Management
Mar 8, 2013
79
US
Code:
If Substr(Alltr(field1),1,1)='#' Or Lower(Substr(Alltr(field1),1,2))='rm' Or Lower(Substr(Alltr(field1),1,2))='sp' Or Lower(Substr(Alltr(field1),1,3))='apt' Or Lower(Substr(Alltr(field1),1,3))='lot' Or Lower(Substr(Alltr(field1),1,3))='bld' Or Lower(Substr(Alltr(field1),1,3))='pmb' Or Lower(Substr(Alltr(field1),1,3))='apa' Or Lower(Substr(Alltr(field1),1,3))='ste' Or Lower(Substr(Alltr(field1),1,4))='buil Or Lower(Substr(Alltr(field1),1,4))='room' Or Lower(Substr(Alltr(field1),1,4))='unit' Or Lower(Substr(Alltr(field1),1,4))='suit'

Won't execute this statement, because of too many ORs. Any idea to work this around without splitting it into 2 separate ones or maybe more efficient solution for the same task?!
 
First, you forgot to close a string here
Lower(Substr(Alltr(field1),1,4))='buil

Second, it can be shortened :
- SUBSTR(ss,1,nn) WITH LEFT(ss,nn)
- xx = 'aa' or xx = 'bb' with INLIST(xx,'aa','bb')
Code:
If LEFT(Alltr(field1),1)='#' Or INLIST(Lower(LEFT(Alltr(field1),2)),'rm','sp') Or INLIST(Lower(LEFT(Alltr(field1),3))='apt','lot','bld','pmb','apa','ste') Or INLIST(Lower(LEFT(Alltr(field1),4)),'buil','room','unit','suit')

Supplementary, you can perform lcMyField = LOWER(ALLTRIM(field1)) before IF
Code:
lcMyField = LOWER(ALLTRIM(field1))
If LEFT(m.lcMyField,1)='#' Or INLIST(LEFT(m.lcMyField,2),'rm','sp') Or INLIST(LEFT(m.lcMyField,3)='apt','lot','bld','pmb','apa','ste') Or INLIST(LEFT(m.lcMyField,4),'buil','room','unit','suit')

And, if you like, you can evaluate individual conditions, before IF
Code:
lcMyField = LOWER(ALLTRIM(field1))
llC1 = LEFT(m.lcMyField,1)='#'
llC2 = INLIST(LEFT(m.lcMyField,2),'rm','sp')
llC3 = INLIST(LEFT(m.lcMyField,3)='apt','lot','bld','pmb','apa','ste')
llC4 = INLIST(LEFT(m.lcMyField,4),'buil','room','unit','suit')
IF m.llC1 OR m.llC2 OR m.llC3 OR m.llC4

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Which do you think the most efficient way?
Did you do it on purpose- in 4th line have "=", but 3rd and 5th have ","
Code:
llC2 = INLIST(LEFT(m.lcMyField,2)[COLOR=#CC0000],[/color]'rm','sp')
llC3 = INLIST(LEFT(m.lcMyField,3)[b][COLOR=#CC0000]=[/color][/b]'apt','lot','bld','pmb','apa','ste')
llC4 = INLIST(LEFT(m.lcMyField,4)[COLOR=#CC0000],[/color]'buil','room','unit','suit')
 
Linousa said:
Did you do it on purpose- in 4th line have "=", but 3rd and 5th have ","
Sorry for that, you're right, it was a copy-paste error.
Obviously
Code:
INLIST(LEFT(m.lcMyField,3)='apt','lot','bld','pmb','apa','ste')
is wrong and
Code:
INLIST(LEFT(m.lcMyField,3),'apt','lot','bld','pmb','apa','ste')
is correct

I believe the second is the less time consuming, because it avoids to repeatedly evaluate LOWER(ALLTRIM(field1)), and skips unnecessary assignments like llC1 = LEFT(m.lcMyField,1)='#'
Code:
lcMyField = LOWER(ALLTRIM(field1))
If LEFT(m.lcMyField,1)='#' Or INLIST(LEFT(m.lcMyField,2),'rm','sp') Or INLIST(LEFT(m.lcMyField,3),'apt','lot','bld','pmb','apa','ste') Or INLIST(LEFT(m.lcMyField,4),'buil','room','unit','suit')
But it matters only inside a loop


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Linousa,

I won't try to answer your question, as Vilhelm-Ion has already given you a lot of useful help.

But I will make a request. When you post a long line of code, please use line breaks to split it into shorter line. The line that you posted is 540 characters long, and it is impossible to view - or to understand - witout constant horizontal scrolling.

You can split a line of code into shorter lines by placing a semi-colon at the end of each line (except the last).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
MikeLewis said:
When you post a long line of code, please use line breaks to split it into shorter line.

Sure, in case, it pokes someone else eye(also legit for FoxPro):
Code:
                If Substr(Alltr(field1),1,1)='#' ;
    Or Lower(Substr(Alltr(field1),1,2))='rm' ;
    Or Lower(Substr(Alltr(field1),1,2))='sp' ;
    Or Lower(Substr(Alltr(field1),1,3))='apt' ;
    Or Lower(Substr(Alltr(field1),1,3))='lot' ;
    Or Lower(Substr(Alltr(field1),1,3))='bld' ;
    Or Lower(Substr(Alltr(field1),1,3))='pmb' ;
    Or Lower(Substr(Alltr(field1),1,3))='apa' ;
    Or Lower(Substr(Alltr(field1),1,3))='ste' ;
    Or Lower(Substr(Alltr(field1),1,4))='buil';
    Or Lower(Substr(Alltr(field1),1,4))='room';
    Or Lower(Substr(Alltr(field1),1,4))='unit';
    Or Lower(Substr(Alltr(field1),1,4))='suit'
 
I know this has long since been resolved but I'm just adding a comment here that VFP 9.0 can handle that many OR statements, actually more. I added additional OR statements out to 998 characters in the command window and it worked. I added even more OR statements in a PRG and it worked. Visual FoxPro System Capacities lists the command line limit at 8192 characters, nothing about a limit for AND/OR/etc operations. So unless I hear otherwise I'm concluding 'OR' wasn't the real issue or perhaps linousa is using an earlier version of VFP.
 
As far as I know, there is no theoretical limit on the number of ANDs or ORs in a single statement - other than what is imposed by the length of a single line (which, as dbMark rightly says, is 8,192 chars).

In fact, I don't recall there ever being a limit, even in FoxBase or dBASE.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
dbMark said:
I know this has long since been resolved but I'm just adding a comment here that VFP 9.0 can handle that many OR statements, actually more. I added additional OR statements out to 998 characters in the command window and it worked. I added even more OR statements in a PRG and it worked. Visual FoxPro System Capacities lists the command line limit at 8192 characters, nothing about a limit for AND/OR/etc operations. So unless I hear otherwise I'm concluding 'OR' wasn't the real issue or perhaps linousa is using an earlier version of VFP.

Interesting... Just to clarify, because I have FoxPro 9.0 SP2 Version 09.00.0000.5815
Untitled_ewvgmg.png


So what is this then? Whole line is 540 characters long and at character 409 and further it greys out?!
Untitled2_tislto.png
 
Look at 'buil, the end delimiter is missing.
This was already the first answer you got from Vilhelm-Ion Praisach

Bye, Olaf.
 
Alright people, you are all 100% right! I just copy-paste it from my initial question and it was my fault, I admit it! Thank you everyone with this question, great job. [hourglass]
 
As far as I know, there is no theoretical limit on the number of ANDs or ORs in a single statement - other than what is imposed by the length of a single line (which, as dbMark rightly says, is 8,192 chars).

Well, there is one other esoteric limit. It causes error 1252, "Compiled code for this line is too long".

We don't have any control over the line of compiled code, or course. It's controlled by the complexity of the code when it is compiled. But we haven't seen this error reported in years so I suspect they either vastly increased the limit, or removed it entirely.

As others have said, in this thread there's a missing string delimiter not a line length problem.
 
Just because FOR and WHERE conditions are about logical expressions you process with AND and OR conditions? No, SYS(3055) is about stack space needed for a number of fields. It's closely related, but the culprit here is and was an unclosed string.

Brackets and other delimiters missing can easily mess up the compiler and lead to misleading error messages.
Do yourself a favor and in Tools->Options go to the Editor Tab and set string coloring red like this:
stingcoloring_pnib7u.jpg


Now your wrong line of code will look like this:
codeerror_m6mqng.png

(Edit: I am not adding semicolons here, as I just wanted to demonstrate the syntax coloring effect)

Now, Lino, if that doesn't jump into anyones eye, jump right through it directly into the brain and cause a DOH there, then I don't know. You don't have to be a genius, nerd or autistic person to see this.

Bye, Olaf.

Edit2: Besides, if I try to compile this, after adding the semicolons, I get the correct error "command contains unrecognized phrase/keyword", this also should tell you something like a bracket, delimiter or semicolon is missing and messes with the expression. Don't expect a parser to have the intelligence to see an added string delimiter here heals the code and so don't expect to be told there is a string delimiter missing. In fact it's not missing, somewhere after the cutoff there is the next string 'room', and it's first delimiter is taken as the end delimiter of the undelimited 'buil string. So room now is outside of string delimiters and it's the unrecognized phrase or keyword:
codeerror2_ltytrp.png


See?
 
OlafDoschke said:
Now, Lino, if that doesn't jump into anyones eye, jump right through it directly into the brain and cause a DOH there, then I don't know. You don't have to be a genius, nerd or autistic person to see this
Don't you think it's kinda overwhelming answer for Q&A? I wonder - what you smoking?! Still bagging you that bad 11 days later, really?! =) I had my answer from vgulielmus, 44 minutes later. I hope you will get much cooler about questions next time and for that - thank you Olaf!
 
Olaf - I knew SYS(3055) wasn't an issue for the OP. But then there was discussion about what limits actually exist.

However, you have a good point that we weren't actually talking about commands involving FOR or WHERE here. That said, Help is pretty clear that SYS(3055) does affect a ton of Xbase commands, not just SQL commands.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top