if and or

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 859
Joined: 29 Sep 2013, 22:18

if and or

10 Nov 2013, 06:55

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 =
	}	
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: if and or

10 Nov 2013, 10:29

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
User avatar
tidbit
Posts: 1166
Joined: 29 Sep 2013, 17:15
Location: USA

Re: if and or

10 Nov 2013, 12:48

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).
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
PuzzledGreatly
Posts: 859
Joined: 29 Sep 2013, 22:18

Re: if and or

10 Nov 2013, 20:43

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.
User avatar
emmanuel d
Posts: 39
Joined: 17 Nov 2013, 04:45

Re: if and or

26 Nov 2013, 15:17

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 "|")
User avatar
AfterLemon
Posts: 83
Joined: 30 Sep 2013, 14:27
Location: Arizona, USA

Re: if and or

27 Nov 2013, 15:33

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.
Last edited by AfterLemon on 27 Nov 2013, 19:10, edited 2 times in total.
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: if and or

27 Nov 2013, 16:27

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)
User avatar
AfterLemon
Posts: 83
Joined: 30 Sep 2013, 14:27
Location: Arizona, USA

Re: if and or

27 Nov 2013, 19:09

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.
HOME: Windows 7 x64 SP1 Ultimate | AMD FX 8350 8-Core @ 4.00GHZ | 16GB Ram
WORK: Windows 7 x64 SP1 Home Premium | AMD Athlon II X4 640 @ 3.00GHz | 8GB Ram
MOBILE: Samsung Galaxy Note 3 | 2012 Asus Nexus 7 | Zune 80Gb
FanaticGuru
Posts: 1406
Joined: 30 Sep 2013, 22:25

Re: if and or

27 Nov 2013, 19:30

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: if and or

28 Nov 2013, 19:21

FanaticGuru wrote:Now everyone is right and everyone is happy.
FG
lol nice work :D
User avatar
PuzzledGreatly
Posts: 859
Joined: 29 Sep 2013, 22:18

Re: if and or

28 Nov 2013, 20:05

Thanks, everyone.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], BobbitWormJoe, FBurns, Google [Bot], Ian, loek6000, popwojciu, Xtra and 256 guests