Sign in to follow this  
Followers 0
TechCoder

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

6 posts in this topic

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.

Share this post


Link to post
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!

Share this post


Link to post
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 &
~~~~~~~~~~~~~~~~~~~~~~^

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

  • Similar Content

    • XinYoung
      By XinYoung
      Hello again,
      I'm trying to use the FileMove function to rename a bunch of text files.
      Some of the files have "_1" at the end of their name, for example, "File123_1.txt". I want to remove the "_1" so it would become "File123.txt".
      What I currently have...
              FileMove($sSource & "\*_1.txt", $sDestination & "\*.txt")
      But since I'm using a wildcard, it doesn't appear to be working. I think it's just replacing .txt with .txt. Am I going about this the wrong way? How can I use wildcards and still accomplish this?
      Any help is greatly appreciated ^__^
    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
    • kartune
      By kartune
      Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.
      Here is an example:
       
      <autoit>
      Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ
      GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
      </autoit>
      Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.
      I've already tried
      <autoit>
      Assign($clear1, $SATJ)
      </autoit>
       
      any help is greatly appreciated!!! 
    • rkr
      By rkr
      Hi, i want to read a particular string from a text file using autoit. i wish to read it without explicitly opening the text file. the copied string should be then transferred to an excelbook (again, no need to explicitly open the excel book)... 

      with reference to my screenshot attached, my input to the  'script'' is going to be 0017-0008, and the script should copy the highlighted two lines from the input file to excel
      thanks

    • Atoxis
      By Atoxis
      Howdy, this is my first post, massive fan of autoit. 
      I've searched and tried and I would just like people who are better at this than me to let me know if this is even a thing.

      I'd like to perform just a variable. For example, it would be. *see inserted code*
      So what i'm wanting is, create the constant $test, and that variable would be what is followed after the = . Then perform the _FileCreate. Then perform the variable.  Logically or in my head rather.. That variable is declared and is equal to what it is set to above, therefore just placing the variable plainly in the script, it should be equal to what it was declared as.  So what am I doing wrong, and or how can I have autoit just perform the variable.  

       
      #include <File.au3> Const $test = FileWriteLine(@DesktopDir & "\Log.txt", @CRLF ) _FileCreate(@DesktopDir & "\Log.txt") $test