Is there a simplified way to extract the variable?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Is there a simplified way to extract the variable?

17 Nov 2019, 12:17

I am able to find the correct variable using a loop...

Code: Select all

Var1 := 0
Var2 := 1
Var3 := 0

Loop 3
	if (Var%A_Index% = 1)
		VarX := "Var" A_Index
	MsgBox % VarX
I am also able to find the variable this way...

Code: Select all

VarX := (Var1 = 1) ? "Var" 1 : (Var2 = 1) ? "Var" 2 : (Var3 = 1) ? "Var" 3
	MsgBox % VarX
but is there a simpler or shorter way?
User avatar
Chunjee
Posts: 1501
Joined: 18 Apr 2014, 19:05
Contact:

Re: Is there a simplified way to extract the variable?

17 Nov 2019, 12:39

Worth learning to use an array. the Psudoarray stuff (Var%A_Index%) is bad practice. Your loop has no easy way to know there are 3 values to check.

Code: Select all

array := [0, 1, 0]
msgbox, % FindInArray(array, 1)
; => 2

FindInArray(param_array, param_searchFor) {
    loop, % param_array.Count() {
        if (param_searchFor == param_array[A_Index]) {
            return A_Index
        }
    }
}
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Is there a simplified way to extract the variable?

17 Nov 2019, 13:08

I think I understand, but how can it be used in a GUI?

Code: Select all

Gui, Add, Checkbox, gMySub vVar1 X10, Choice A
Gui, Add, Checkbox, gMySub vVar2 X+10, Choice B
Gui, Add, Checkbox, gMySub vVar3 X+10, Choice C
Gui, Show
return

MySub:
Gui, Submit, NoHide
; Find out which box is checked, and store as Var1, Var2, or Var2
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Is there a simplified way to extract the variable?

18 Nov 2019, 03:41

Hallo,
try:

Code: Select all

Gui, Add, Checkbox, gMySub vVar1 X10, Choice A
Gui, Add, Checkbox, gMySub vVar2 X+10, Choice B
Gui, Add, Checkbox, gMySub vVar3 X+10, Choice C
Gui, Show
return

MySub:
Gui, Submit, NoHide
; Find out which box is checked, and store as Var1, Var2, or Var2
While, (VarX:="Var" A_Index) And (Checked:=StrLen(%VarX%)) And !%VarX%
	Continue
ToolTip,% Checked?VarX:"no"
Return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is there a simplified way to extract the variable?

20 Nov 2019, 02:17

For this very specific case, you could make use of logarithms to produce a one-liner:

Code: Select all

;q:: ;report the biggest variable number that contains 1 (where each variable contains 0 or 1)
Var1 := 0
Var2 := 1
Var3 := 0
vNum := Var1<<1 | Var2<<2 | Var3<<3
MsgBox, % vNum ? ("Var" Floor(Log(vNum)/Log(2))) : ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 347 guests