Sign in to follow this  
Followers 0
PsaltyDS

Variable variables?

10 posts in this topic

I have a 2-D array which is displayed by a GUI for the operator to pick one of the values. That all works great.

Once the operator picks a value, i.e "ABC" or "XYZ", I want to present options from another of several 2-D arrays, i.e. $avABC_Options or $avXYZ_Options.

Once I have a variable containing the operator's selection, i.e. $Selection, how do I use it to address the next array? I can assemble the name of the next array to use as a string:

$sNextArray = "$av" & $Selection & "_Options"

That will make $sNext Array = "$avABC_Options" or "$avXYZ_Options", but how do I use this variable to address the array variable? Lets say I want the value at [2][3] of the selected array; the following does not work:

$sNextArray[2][3]

So how do I use one variable to access the slected array?

P.S. If it was really only the two options, I could just duplicate all the logic within a couple of Case selections, but in the actual application the first array would be used to select values from 15 or more other arrays. I am also considering just going to higher numbers of dimensions in the first array, but if I can call other arrays using a variable assigned from the first, it will be easier to maintain.


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites



I'm not clear on whether you're looking to get another array, or simply a value. Look at the help on the Eval function. I did a quick and dirty test, and for the line you have above:

$sNextArray = "$av" & $Selection & "_Options"

the following might work for you:

$sNextArray = Eval ( "av" & $Selection & "_Options" )

Also look at the Assign function that allows you to create variables and assign them values on the fly. Not sure if it applies here, but I just saw it myself, and thought it was very cool and might be helpful!

Share this post


Link to post
Share on other sites

in other languages, a variable for a variable is pointer and autoit doesn't have them to my knowledge. could you just add another dimension to the array?

$myarray[$arraynumber][$col][$row]
I was thinking "Indirect Address", which is a machine/asembler term, but "Pointer" works. Now I want pointers in AutoIT!

:-)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I'm not clear on whether you're looking to get another array, or simply a value. Look at the help on the Eval function. I did a quick and dirty test, and for the line you have above:

the following might work for you:

$sNextArray = Eval ( "av" & $Selection & "_Options" )

Also look at the Assign function that allows you to create variables and assign them values on the fly. Not sure if it applies here, but I just saw it myself, and thought it was very cool and might be helpful!

Eval! Eureka! Ah Ha! Hallaluah! JerryD rocks! Well, sort of...

It appears to work for variables but not for arrays. Example code:

#include <array.au3>

; Setup variables
$a = "x"
$x_var = "One"

; Setup array
Dim $One_Example[2]
$One_Example[0] = "One"
$One_Example[1] = "Two"

; Test variable addressing a variable
$var1 = Eval($a & "_var")
MsgBox(0, "Variable addressing variable test", "$x_var = " & $x_var & @CRLF & "$var1 = " & $var1)

; Test variable addressing an array
$var2 = Eval($var1 & "_Example[1]")
MsgBox(0, "Variable addressing array test", "$One_Example[1] = " & $One_Example[1] & @CRLF & "$var2 = " & $var2)

I specificaly need to access a bunch of arrays, using variables to define which array and what coordinates in the array. For example:

;  The follwing array access:
$Result = $One_Example[1][2]

;  Should give the same result as:
$ArrayName = One_Example
$Row = "1"
$Col = "2"
$Result = Eval($ArrayName & "[" & $Row  & "][" & $Col & "]")

Am I missing something...?


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

;  The follwing array access:
$Result = $One_Example[1][2]

;  Should give the same result as:
$ArrayName = One_Example
$Row = "1"
$Col = "2"
$Result = Eval($ArrayName & "[" & $Row  & "][" & $Col & "]")

Am I missing something...?

missing quotes around "One_Example" when assigning it to $ArrayName

Share this post


Link to post
Share on other sites

If you are using Eval for arrays... why not use pseudo arrays...

Assign("array_0_0","test")

$b = 0

$c = 0

$result = Eval("array_" & $b & "_" & $c)

Lar.

Hmm... pseudo arrays... hmm... :P

I guess that would work, but seems unwieldy for my application:

1. I have a 2-D array with some (currently 15, maybe more in future) selections, and a text description for each. Works great, easy to update.

2. The array is presented to the user in a GUI ListView.the operator selects one and clicks OK. My first AutoIT GUI and it works great.

3. For each selection from the first array, there is another 2-D array with between 3 and 75 secondary selections, and 5 or 6 colums of attributes for each. Logical (to me any way) and easy to update, since a single DataArrays.au3 file contains all of theses arrays and are added by "#include <DataArrays.au3>" to the main script. BTW, DataArrays.au3 is currently over 1200 lines.

4. Next is a second GUI to present the secondary selections for the operator to choose. This is where it blows up. I need to address all those secondary arrays using a variable.

If Eval() worked on arrays, I'd be set! Any reason why it can't?


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

missing quotes around "One_Example" when assigning it to $ArrayName

Oops, true enough... :P

But fixing that doesn't impact the problem. I think the strings are properly quoted in the running example I provided right above that.


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

i think i got it. when you eval() it, don't put the dimensions in the eval. instead of using:

eval($arrayname & "_" & $row & "_" & $col & "[" & $item & "]")

use:

$temp = eval($arrayname & "_" & $row & "_" & $col)
$data = $temp[$item]

that make sense?

working sample:

dim $myarray5_6[3]
$myarray5_6[2] = "cow"
$arrayname = "myarray"
$arraycol = 5
$arrayrow = 6
msgbox(0,"Testing",$myarray5_6[2])
$temp1 = eval($arrayname & $arraycol & "_" & $arrayrow & "[2]")
msgbox(0,"Testing",$temp1)
$temp2 = eval($arrayname & $arraycol & "_" & $arrayrow)
msgbox(0,"Testing",$temp2[2])

Share this post


Link to post
Share on other sites

I can use that! :)

Your example seems to be trying to use a pseudo array, which is not what I want, but your last couple of lines gave me an answer. First, I take out the pseudo row/col stuff:

; Original
$temp2 = eval($arrayname & $arraycol & "_" & $arrayrow)
msgbox(0,"Testing",$temp2[2])

; Redacted version
$temp = eval($arrayname)
msgbox(0,"Testing",$temp[2])

:P What's happening here is a COPY of the entire array to one with a pre-defined name (temp).

I have created a demo script with two 2-D arrays that are randomly accessed by variables. One array contains a list of real animals and their attributes, the other has mythical animals. Either array could be of unkown length, so [0][0] is defined by convention as the max index. A random number varialble picks the array (real or mythical) and another random number variable picks which animal in the array. The array to be used is defined by $ArrayName, and the Eval() results in the selected array being copied to $ArrayCopy. As follows:

; Demonstrate variable access of 2-D arrays
#include <Array.au3>

; Init arrays
Dim $Real_Animals[3][3]
$Real_Animals[0][0] = UBound($Real_Animals) - 1
$Real_Animals[1][0] = "cow"
$Real_Animals[1][1] = "brown"
$Real_Animals[1][2] = "vegitarian"
$Real_Animals[2][0] = "tiger"
$Real_Animals[2][1] = "orange"
$Real_Animals[2][2] = "carnivore"

Dim $Mythical_Animals[4][3]
$Mythical_Animals[0][0] = UBound($Mythical_Animals) - 1
$Mythical_Animals[1][0] = "dragon"
$Mythical_Animals[1][1] = "green"
$Mythical_Animals[1][2] = "carnivore"
$Mythical_Animals[2][0] = "unicorn"
$Mythical_Animals[2][1] = "white"
$Mythical_Animals[2][2] = "vegitarian"
$Mythical_Animals[3][0] = "hypogriff"
$Mythical_Animals[3][1] = "grey"
$Mythical_Animals[3][2] = "carnivore"

; Display a random animal
While (1)
    If Random() < 0.50 Then
        $A1 = "Real"
    Else
        $A1 = "Mythical"
    EndIf
    $ArrayName = $A1 & "_Animals" ; $ArrayName is either "Real_Animals" or "Mythical_Animals"
    $ArrayCopy = Eval($ArrayName) ; The Eval() function copies the entire selected array to $ArrayCopy
    $B1 = Random(1, $ArrayCopy[0][0], 1)
    If MsgBox(1, "Animal Characteristics", "From the " & $A1 & " world:" & @CRLF & "The " & $ArrayCopy[$B1][0] & " is a " & $ArrayCopy[$B1][1] & " " & $ArrayCopy[$B1][2] & ".") = 2 Then
        Exit
    EndIf
WEnd

That works! :(

My only question is if this is efficient? Every itteration of the above script has to copy the entire contents of the source array to $ArrayCopy. Can I put in for a new feature where Eval() can access an array directly without having to copy it first?

:lmao:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0