How do I use the contents of an array as a variable name? - autohotkey

Currently I store the contents of an array into a variable, then set that variable up using two % and store a value. My current code looks like this:
Test := {asdf: "blah"}
Temp := Test["asdf"]
%Temp% := "boo"
; above line is be the same as blah := "boo", but blah came from a variable
msgbox %blah% ; outputs "boo"
I don't like having to use the Temp variable like this.
The following compiles but blah stays blank:
(Test["asdf"]) := "boo"
%Test%["asdf"] := "boo"
The following gives me a compile error:
%(Test["asdf"])% := "boo"
I have a vague idea that it should be possible but I just can't find the syntax for it. How do I directly use the array instead of having to put it in a temp variable?

Just figured it out.
The problem here is creating variables using dynamic data may cause invalid variable names to be created. All sorts of ways to screw up here (spaces, UTF-8 code, etc).
One safer way is to use Associative Arrays:
Output := Object()
Test := {asdf: "blah"}
Output[(Test["asdf"])] := "boo"
msgbox % Output["blah"]
There are less restrictions on keys than variable names.

globalWrapper(NameOfTheGlobalVar, LocalVar) {
global
%NameOfTheGlobalVar% := LocalVar
}
Test := {asdf: "blah"}
globalWrapper(Test["asdf"], "boo")
msgbox %blah% ; outputs "boo"

Related

Multi-variable assignment to an array in TwinCAT

How can assign a new variable to an array in TwinCAT?
in TwinCAT you can initialize all your array's argument directly for example for array a we can use:
a : ARRAY [1..3] OF INT := [3(0)];
or
a : ARRAY [1..3] OF INT := [0,0,0];
but if you want to assign the array in the main program(not initializing part) for example
a:=[2,8,5];
you will face this error tip: Unexpected array initialisation.
any help would be appreciated.
You cannot directly initialize arrays inside the program part.
That beeing said, the best option is porbably to define a constant containing the desired initialization values and then assigning the value of that constant to your array:
VAR
aiMyArray : ARRAY [1..2] OF INT;
END_VAR
VAR CONSTANT
aiInitializerMyArrayOptionA : ARRAY [1..2] OF INT := [1,2];
aiInitializerMyArrayOptionB : ARRAY [1..2] OF INT := [3,4];
END_VAR
IF bCondition THEN
aiMyArray := aiInitializerMyArrayOptionA;
ELSE
aiMyArray := aiInitializerMyArrayOptionB;
END_IF
The other option would be to manually initialize each index one by one which easily gets impracticale with decent array sizes:
IF bCondition THEN
aiMyArray[1] := 1;
aiMyArray[2] := 2;
ELSE
aiMyArray[1] := 3;
aiMyArray[2] := 4;
END_IF
Looping thorugh all elements and assigning values might be an option too. But that would only be usefull when the values are no arbitrary constatns but you are able to calculate the values from some formula.

What happens when I try to copy an object with `thecopy := object` instead of object.Clone()?

thearray := [6,77,4,3,66,11]
thecopy := thearray
MsgBox % thecopy.Length() ; 6
thearray := function(thearray)
MsgBox % thecopy.Length() ; 0
MsgBox % thearray.Length() ; 6
Why is thecopy "ruined" when thearray is changed by a function? My guess: The function changes the thearray. Therefor thecopy, which I guess is some kind of reference (?) to the same array as 'thearray' was, is invalid or something. Is this the right way to think about this? I'm all new to this reference thing or whatever it is.
And by the way, if .Clone() is added in the end of the second row thecopy remains intact.

Is it possible to initialize array in batch in AutoHotKey?

I know I can write
a := Object()
a[1] := "textA"
a[2] := "textB"
a[3] := "textC"
Can I write something like
a := {"textA", "textB", "textC"}
?
You can define an indexed array using the bracket syntax:
a := ["textA", "textB", "textC"]
or the array creation function:
a := Array{"textA", "textB", "textC"}
An indexed array is an object representing a list of items, numbered 1 and up. In this example, the value "textA" is stored in object key 1, the value "textB" in object key 2 and the value "textC" in object key 3.
https://autohotkey.com/docs/Tutorial.htm#s7

How to convert DOM::MATRIX to Function

A simple example of what I am doing is as follows:
s := t*0.2:
b := matrix([0.5,0.6,0.1]):
f := matrix([sin(s),cos(s),s]):
X := transpose(b)*(f);
plotfunc2d(X,t=-2*PI..2*PI);
The error is:
Error: Expecting an arithmetical expression or a function. Got a 'Dom::Matrix()' for attribute 'Function' in the 'Function2d' object.
so, I need to convert types from Dom::Matrix to Function. I have tried:
coerce(X,DOM_EXPR);
I know that simply, this works:
s := t*0.2:
x := 0.5*sin(s)+0.6*cos(s)+0.1*s;
plotfunc2d(x,t=-2*PI..2*PI);
Is there a way to convert these types?
While the variable X itself is a DOM::Matrix(), the single element it contains is an arithmetical expression, so you need to pass the element itself to the plot function:
plotfunc2d(X[1],t=-2*PI..2*PI);

How can I use a variable as a key in the associative array in autohotkey?

In autohotkey_L, there is a associative data structure. For example,
hash := {key_hash:"value"}
val:= hash["key_hash"]
MsgBox %val%
But if I want to use a variable as a key to access the value in the assocative array, it fails. For example, the following doesn't work
hash := {key_hash:"value"}
other_val="key_hash"
val:= hash[other_val]
MsgBox %val%
and this doesn't work either:
hash := {key_hash:"value"}
other_val="key_hash"
val:= hash[%other_val%]
MsgBox %val%
** gave me an error: The following variable name contains an illegal character: ""key_hash""
How can I use a variable to access the value in an associative array?
I need this to get the key as an argument in a function.
Alby,
Your variable other_val contained the data: "key_hash" , not what you wanted: key_hash. Just remove the two double quotes and you are fine.
hash := {key_hash:"value"}
other_val=key_hash
val:= hash[other_val]
MsgBox %val%
Or use the assignment (:=)
hash:={key_hash:"value"} ; hash:=Object("key_hash", "value")
other_val:="key_hash"
val:=hash[other_val]
MsgBox, % val