Jump to content

dynamically building a reference to a variable ($$string type)


Go to solution Solved by jaberwacky,

Recommended Posts

Haven't come across this 'feature (or is it a trick?)' in the forum and not sure how to go about making it work in AutoIt.....

I'm trying to 'build' the variable string dynamically before referencing it.

#include <Array.au3>

Local $TOOLS[10]
Global Enum $ID, $output1,$output2,$output3,$output4
$TOOLS[1] = "one"
$TOOLS[2] = "two"
$TOOLS[3] = "three"
$TOOLS[4] = "four"

Local $inout = "out"

For $x = 1 to UBound($TOOLS)
    Local $string = $inout & "put" & $x
    ConsoleWrite($TOOLS[$($string)] & @crlf)
Next

(this was 'latest attempt' - tried several ways to build the $$string, always get a syntax error)

What I want to do is use a loop to go through several (more than shown) variables and not have to type out every one.

The way I've done it in php is just to build the $string and then put a $ in front of it (i.e. $$string).

In AutoIt I have achieved the result using AssocArray* functions.

For $i = 1 To 9
    $toolbox = $inout & "put" & $i
    Local $type = AssocArrayGet($TOOLS, $toolbox)
    Switch $type
.
.

While I find AssocArray* 'easy', I have found them to be causing some slowdowns that I would rather avoid, so I'm trying to replace all those functions and this is the last one I have to do.

I'm just learning about Enum variables and see there is a lot of value in using them, though I haven't yet learned perhaps the 'best' way to use them.....

If it is possible to 'build' the $$string type variable, I'll be able to replace the last bits of the AssocArray* functions so I can retest for slowdowns.

Thanks for input on this.

Link to comment
Share on other sites

I think you want Eval(...).

Worked a CHAMP!

#include <Array.au3>

Local $TOOLS[10]
Global Enum $output1,$output2,$output3,$output4
$TOOLS[1] = "one"
$TOOLS[2] = "two"
$TOOLS[3] = "three"
$TOOLS[4] = "four"

for $x = 1 to UBound($TOOLS)
    Local $string = "output" & $x
    ConsoleWrite($TOOLS[Eval("output"&$x)] & @crlf)
Next

(still need to adjust things a bit, but the concept is there.....)

Thanks!

Link to comment
Share on other sites

in this case, though, the new variable was created in the Enum, so all I needed to do was reference to that, which Eval does as I needed, and works like I've always used $$string (where $string has always been created first in my use, though php would create a new one, if you wanted - that is a difference......)

AutoIt doesn't create one

Local Eval("thisnewone") = "works"
ConsoleWrite($thisnewone & @crlf)

gives

 

error: syntax error

Local Eval
~~~~~~^
 
error: $thisnewone: undeclared global variable.
ConsoleWrite($thisnewone &
~~~~~~~~~~~~~~~~~~~~~~^
Link to comment
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
 Share

×
×
  • Create New...