Jump to content
Sign in to follow this  
TechCoder

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

Recommended Posts

TechCoder

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
TechCoder

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
TechCoder

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  

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • xiantez
      By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
×