Page 1 of 1

How to use different variables in the same "if"?

Posted: 02 Jun 2018, 15:14
by rdllngr
I have several variables that need to go to their respective labels. Instead of repeating "multiple ifs" for each variable, is there a way to group them into a "single if"?

I need to learn how turn this:

Code: Select all

if cKey = Main_B
	gosub, Last_Key
if cKey = Main_C
	gosub, Last_Key
if cKey = Main_D
	gosub, Last_Key
if cKey = Main_E
	gosub, Last_Key
if cKey = Main_F
	gosub, Last_Key
...Into this (or similar):

Code: Select all

if cKey = Main_B or Main_C or Main_D or Main_E or Main_F
	gosub, Last_Key

Re: How to use different variables in the same "if"?

Posted: 02 Jun 2018, 15:39
by swagfag
what are main_a-z, are those string literals?

Code: Select all

Keys := {}
for each, suffix in StrSplit("B,C,D,E,F", ",")
	Keys["Main_" suffix] := "LastKey"

MsgBox % "trying valid"
cKey := "Main_D"
if (IsLabel(label := Keys[cKey]))
	Gosub % label

MsgBox % "trying invalid"
cKey := "not Main_whatever"
if (IsLabel(label := Keys[cKey]))
	Gosub % label

MsgBox % "script will exit"
Return ; end autoexecute

LastKey:
	MsgBox % "hello from " A_ThisLabel
Return

Re: How to use different variables in the same "if"?  Topic is solved

Posted: 02 Jun 2018, 16:46
by Exaskryz
Looking at the direction you were taking OP, I'd do this.

Code: Select all

If (cKey = "Main_B") OR (cKey = "Main_C") OR (cKey = "Main_D") ...
   gosub, Last_Key
You could also use If var in to do

Code: Select all

If cKey in Main_B,Main_C,Main_D,Main_E,Main_F
    gosub, Last_Key

Re: How to use different variables in the same "if"?

Posted: 02 Jun 2018, 16:58
by rdllngr
@swagfag
Yes, Main_"A-Z" are string literals. My code is very complex, so I needed a simpler solution.

As they are strings, "if var contains" works very well.

My final code:

Code: Select all

if cKey contains Main_B,Main_C,Main_D,Main_E,Main_F
	gosub, Last_Key
Thanks for the answer because it gave me some ideas. Helped me anyway. :D

Re: How to use different variables in the same "if"?

Posted: 02 Jun 2018, 17:01
by jeeswg
Some ideas. Cheers.

Code: Select all

if var in % var1 "," var2 "," var3

if var in literal1,literal2,literal3

if (cKey ~= "^Main_[BCDEF]$") ;RegEx case sensitive

if (cKey ~= "i)^Main_[BCDEF]$") ;RegEx case insensitive
But note: RegEx can be slower, sometimes one RegExMatch/RegExReplace call can be slower than multiple InStr/StrReplace calls.

Re: How to use different variables in the same "if"?

Posted: 02 Jun 2018, 17:04
by rdllngr
Thank you, jeeswg and Exaskryz. Both work fine. :D

Re: How to use different variables in the same "if"?

Posted: 02 Jun 2018, 17:07
by swagfag
mhm the shorthand regex solution looks nice for non-performance critical situations

Re: How to use different variables in the same "if"?

Posted: 03 Jun 2018, 07:30
by DRocks
jeeswg wrote:Some ideas. Cheers.

Code: Select all

if var in % var1 "," var2 "," var3

if var in literal1,literal2,literal3

if (cKey ~= "^Main_[BCDEF]$") ;RegEx case sensitive

if (cKey ~= "i)^Main_[BCDEF]$") ;RegEx case insensitive
But note: RegEx can be slower, sometimes one RegExMatch/RegExReplace call can be slower than multiple InStr/StrReplace calls.
WoW man did tout just do a regex function that easily?