## Dynamic Variable/Function?

Get help with using AutoHotkey and its commands and hotkeys
Posts: 225
Joined: 07 Oct 2015, 11:06

### Dynamic Variable/Function?

I am using three variables. Var1 is the baseline which I modify, 2 and 3 are always simply Var1+x. Two questions

1)

What is the simplest way to write

Code: Select all

``````n1:=1
n2:=(n1+1)
n3:=(n1+2)``````
2)

Can we further simplify and define n2 as always n1+1, without writing the whole formula out every time it is called?

I.e. at the top of the script,

" if (n2 is found, find n1 and use n1+1) "

Thank you
ilhom
Posts: 52
Joined: 19 Aug 2019, 17:58

### Re: Dynamic Variable/Function?

12 Sep 2019, 17:17
I am using three variables. Var1 is the baseline which I modify, 2 and 3 are always simply Var1+x. Two questions

1)

What is the simplest way to write

Code: Select all

``````n1:=1
n2:=(n1+1)
n3:=(n1+2)``````
2)

Can we further simplify and define n2 as always n1+1, without writing the whole formula out every time it is called?

I.e. at the top of the script,

" if (n2 is found, find n1 and use n1+1) "

Thank you
Created a function of ArrayCalculate. Anytime n is changed, you would use the function and pass the new value of n to it.

Code: Select all

``````VarList := ArrayCalculate(1)
msgbox % VarList[1] ;Output is 1
msgbox % VarList[2] ;Output is 2
msgbox % VarList[3] ;Output is 3

VarList := ArrayCalculate(5)
msgbox % VarList[1] ;Output is 5
msgbox % VarList[2] ;Output is 6
msgbox % VarList[3] ;Output is 7

ArrayCalculate(n){
return [n, n+1, n+2]
}``````
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: Dynamic Variable/Function?

- AFAIK, I've never had a situation like this, so it's possible that I'd refactor the code to avoid having to do this. (E.g. any function would depend only on variable n1, and n2 and n3 would be irrelevant.)
- If I did do this, I might use a function to update the values each time before using the variables.
- Possibly also, you could use a class. With properties 2 and 3 based on property 1.

Code: Select all

``````q:: ;test - update values
n1 := 5
FuncUpdateValues(n1, n2, n3)
MsgBox, % n1 " " n2 " " n3
n1 := 10
FuncUpdateValues(n1, n2, n3)
MsgBox, % n1 " " n2 " " n3

o := [5]
FuncUpdateArray(o)
MsgBox, % o.1 " " o.2 " " o.3
o := [10]
FuncUpdateArray(o)
MsgBox, % o.1 " " o.2 " " o.3

o := []
FuncUpdateArrayAlt(o, 5)
MsgBox, % o.1 " " o.2 " " o.3
FuncUpdateArrayAlt(o, 10)
MsgBox, % o.1 " " o.2 " " o.3

o := new ClassUpdateValues(5)
MsgBox, % o.1 " " o.2 " " o.3
o.1 := 10
MsgBox, % o.1 " " o.2 " " o.3
return

FuncUpdateValues(n1, ByRef n2, ByRef n3)
{
n2 := n1 + 1
n3 := n1 + 2
}

FuncUpdateArray(o)
{
o.2 := o.1 + 1
o.3 := o.1 + 2
}

FuncUpdateArrayAlt(o, n)
{
o.1 := n
o.2 := o.1 + 1
o.3 := o.1 + 2
}

class ClassUpdateValues
{
__New(value)
{
this.1 := value
}
2
{
get
{
return this.1 + 1
}
}
3
{
get
{
return this.1 + 2
}
}
}
``````
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA