Page 1 of 1

if and or

Posted: 10 Nov 2013, 06:55
by PuzzledGreatly
I always have trouble making or statements with if. For example, the following works but how could I combine the first two ifs with an or? Thanks.

Code: Select all

	if DIS contains OrderSequential
	{
		NS = 1
		listP ++
		NumberSet := listP ","	
	}
	else if STY contains ClearList
	{
		NS = 1
		listP ++
		NumberSet := listP ","	
	}	
	else
	{
		NS = 0
		NumberSet =
	}	

Re: if and or

Posted: 10 Nov 2013, 10:29
by TLM
I never use contains so I'm assuming it does not allow for more than 1 condition on the same line.


This should do the same thing:

Code: Select all

   if InStr(DIS,OrderSequential) || InStr(STY,ClearList)
   {
      NS = 1
      listP ++
      NumberSet := listP ","   
   }
   else
   {
      NS = 0
      NumberSet =
   }
Crushed to single line tern:
NumberSet:=InStr(DIS,OrderSequential)||InStr(STY,ClearList)?((!listP?("",listP:=1):"")listP++",",NS:=1):(NS:=0,NumberSet:="")
(listP inits to 1, to start from 0: listP:=0)

hth

Re: if and or

Posted: 10 Nov 2013, 12:48
by tidbit
from the docs:
The operators "between", "is", "in", and "contains" are not supported in expressions.

Thus, you cannot use OR or || with them. you can only use "if var in/contains list..." all by itself.
Solution, see TLM's post, use an actual IF (Expression).

Re: if and or

Posted: 10 Nov 2013, 20:43
by PuzzledGreatly
Thanks for the replies, the single-line crush is rather over-whelming. I tend to forget about InStr() which is preferable to contains when dealing with single items in a matchlist as in this case. I've added Expressions to my favourites list in the help file for quick reference.

Re: if and or

Posted: 26 Nov 2013, 15:17
by emmanuel d
Also note that instr() will partialy match(contains).
so if you have a list you might include the delimiter in the instr() like pipe delimited items:

Code: Select all

if InStr("|" DIS "|","|" OrderSequential "|") || InStr("|" STY "|","|" ClearList "|")

Re: if and or

Posted: 27 Nov 2013, 15:33
by AfterLemon
In actuality, both of the above recommendations for code are incorrect.

TLMs solution 1 will not work due to the text being seen as a VAR in InStr()
TLMs solution 2 will not work due to badly spaced and braced ternary.
emmanuel ds solution will not work due to the text being seen as a VAR in InStr()

This is a non one-line version that will compare the *var* DIS with the STRINGS you specified.

Code: Select all

If(InStr(DIS,"OrderSequential")||InStr(STY,"ClearList"))   ; Both previous code examples used OrderSequential and ClearList as vars instead of strings.
      NS:=1,NumberSet:=++listP ","   ; ++ListP is used instead of a separate assignment
else NS:=0,NumberSet=""
This is a one-line version (essentially the If is replaced with a "?" and the else is replaced with a ":".

((InStr(DIS,"OrderSequential")||InStr(STY,"ClearList")) ? (NS:=1,NumberSet=++listP ",") : (NS:=0,NumberSet=""))

Both should work identically.

Re: if and or

Posted: 27 Nov 2013, 16:27
by TLM
AfterLemon wrote:This is a non one-line version that will compare the *var* DIS with the STRINGS you specified.

Code: Select all

If(InStr(DIS,"OrderSequential")||InStr(DIS,"ClearList"))   ; Both previous code examples used OrderSequential and ClearList as vars instead of strings.
      NS:=1,NumberSet:=++listP ","   ; ++ListP is used instead of a separate assignment
else NS:=0,NumberSet=""
This is a one-line version (essentially the If is replaced with a "?" and the else is replaced with a ":".

((InStr(DIS,"OrderSequential")||InStr(DIS,"ClearList")) ? (NS:=1,NumberSet=++listP ",") : (NS:=0,NumberSet=""))

Both should work identically.
PuzzledGreatly, where you not trying to see if both DIS and or STY contained text?
If so ignore AfterLemon (don't worry we do it all the time)

Re: if and or

Posted: 27 Nov 2013, 19:09
by AfterLemon
TLM wrote:PuzzledGreatly, where you not trying to see if both DIS and or STY contained text?
If so ignore AfterLemon (don't worry we do it all the time)
Updated my post to reflect the stupid mistake I made.

Re: if and or

Posted: 27 Nov 2013, 19:30
by FanaticGuru
TLM wrote:PuzzledGreatly, where you not trying to see if both DIS and or STY contained text?
If so ignore AfterLemon (don't worry we do it all the time)
@TLM, You are mixing up variables and literals.

Code: Select all

if DIS contains OrderSequential ; <-- OrderSequential is a literal string

if InStr(DIS,OrderSequential) ; <-- OrderSequential is a variable
Your code is not equivalent to the original post and fails.

@AfterLemon

Your fancy trick of doing NumberSet:=++listP or NumberSet=++listP returns a null to listP if listP is a null which it will always be causing your code to fail.

Now that everyone has pointed out everyone's errors:

TLM's code is easy to fix by change the variables OrderSequential and ClearList to literal strings. Somewhere along the way spaces do not appear to be needed for ternary operations any more despite what the docs say.

AfterLemon's code is easy to fix by giving listP an initial value at the start of the script.

Now everyone is right and everyone is happy.

FG

Re: if and or

Posted: 28 Nov 2013, 19:21
by TLM
FanaticGuru wrote:Now everyone is right and everyone is happy.
FG
lol nice work :D

Re: if and or

Posted: 28 Nov 2013, 20:05
by PuzzledGreatly
Thanks, everyone.