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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

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

02 Jun 2018, 15:14

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
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

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

02 Jun 2018, 15:39

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
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

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

02 Jun 2018, 16:46

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
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

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

02 Jun 2018, 16:58

@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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

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

02 Jun 2018, 17:01

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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

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

02 Jun 2018, 17:04

Thank you, jeeswg and Exaskryz. Both work fine. :D
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

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

02 Jun 2018, 17:07

mhm the shorthand regex solution looks nice for non-performance critical situations
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

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

03 Jun 2018, 07:30

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?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 361 guests