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

    • 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  
    • ur
      By ur
      I have a script , during compilation and test execution, it worked perfectly but sometimes I am getting error as "Variable used without being declared."
       
      I understood somewhere in the branching logic this is happening.
       
      But not able to find it exactly.
       
      As I am using multiple include statements.the line number is also not giving accurately.
       

       
      Can anyone suggest what is the approach to resolve this?
    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • FroVN
      By FroVN
      Hi, i have a problem :" can't set the name of file with a special character like: \;/;";|;...  have anyway to short the StringInSrt and Stringreplace? i am using this code but too long
      $title=InputBox(0,'','','')
         if StringInStr($title,'\') or StringInStr($title,'/') or StringInStr($title,':') or StringInStr($title,'*') or StringInStr($title,'?') or StringInStr($title,'"') or StringInStr($title,'<') or StringInStr($title,'>') or StringInStr($title,'|') Then
             $title=StringReplace($title,'\','-')
              $title=StringReplace($title,'/','-')
               $title=StringReplace($title,':','-')
                $title=StringReplace($title,'*','-')
                 $title=StringReplace($title,'?','-')
                  $title=StringReplace($title,'"','-')
                   $title=StringReplace($title,'<','-')
                    $title=StringReplace($title,'>','-')
                     $title=StringReplace($title,'|','-')
         EndIf
       
    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?