foggw

Syntax to "ASSIGN/EVAL" Array variable

7 posts in this topic

Sorry if this was already answered already...

Is it possible to ASSIGN/EVAL array values?

;This works
Global $variable
For $idx = 0 to 1
    Assign("variable" & $idx, "Hello_" & $idx)
    ConsoleWrite($idx & " " & Eval("variable" & $idx ) & @CRLF)
Next

;This doesn't
Global $variable[2]
For $idx = 0 to 1
    Assign("variable[" & $idx & "]", "Hello_" & $idx)
    ConsoleWrite($idx & " " & Eval("variable[" & $idx  & "]") & @CRLF)
Next

Thanks

Share this post


Link to post
Share on other sites



from the help file:

Assign ( "varname", "data" [, flag] )

Parameters

varname The name of the variable you wish to assign. Cannot be an array element.

...

Share this post


Link to post
Share on other sites

from the help file: ...Cannot be an array element.

Thank you.

Too much caffeine, not enough sleep.

Share this post


Link to post
Share on other sites

Sorry if this was already answered already...

Is it possible to ASSIGN/EVAL array values?

;This works
Global $variable
For $idx = 0 to 1
    Assign("variable" & $idx, "Hello_" & $idx)
    ConsoleWrite($idx & " " & Eval("variable" & $idx ) & @CRLF)
Next

;This doesn't
Global $variable[2]
For $idx = 0 to 1
    Assign("variable[" & $idx & "]", "Hello_" & $idx)
    ConsoleWrite($idx & " " & Eval("variable[" & $idx  & "]") & @CRLF)
Next

Thanks

You can't do it directly, by you can Assign/Eval to a known temp variable and work with that:
#include <Array.au3>; Only for _ArrayDisplay()

Global $avNaming[3][2] = [["MyArrayVariable0", 3],["MyArrayVariable1", 4],["MyArrayVariable2", 2]]
Global $avGlobalTemp[1]

; Create arrays and set data
For $n = 0 To UBound($avNaming) - 1
    ReDim $avGlobalTemp[$avNaming[$n][1]]
    Assign($avNaming[$n][0], $avGlobalTemp, 2)
    _SetData($avNaming[$n][0])
Next

; Display results
_ArrayDisplay($MyArrayVariable0, "$MyArrayVariable0")
_ArrayDisplay($MyArrayVariable1, "$MyArrayVariable1")
_ArrayDisplay($MyArrayVariable2, "$MyArrayVariable2")

Func _SetData($sVarName)
    Local $avTemp = Eval($sVarName)
    If IsArray($avTemp) Then
        ConsoleWrite("OK" & @LF)
        For $i = 0 To UBound($avTemp) - 1
            $avTemp[$i] = "This is: " & $i
        Next
        Assign($sVarName, $avTemp)
    EndIf
    Return
EndFunc  ;==>_SetData

:D


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

A workaround that may help somebody: eval does not work with arrays as stated above but execute does. I needed this trick to make functions that receive as parameter an array that can have any dimensions (1...64). Unfortunately, this is not a workaround for assigning arrays :-(

Global $variable[2]

For $idx = 0 to 1

$variable[$idx]="Hello_" & $idx

ConsoleWrite($idx & " with execute => " & Execute("$variable[" & $idx & "]") & @CRLF) ; does work

ConsoleWrite($idx & " with eval => " & Eval("variable[" & $idx & "]") & @CRLF) ; doesn't work

Next

Ludvic.

Share this post


Link to post
Share on other sites

Unfortunately, this is not a workaround for assigning arrays :-(

I know this is old but I just found this answer. It does work for assigning arrays too.

Use Execute instead of Eval.

Local $this_is_array[2] = ["one","two"]
Local $varname = "$this_is_array"
Local $tempA = Execute($varname)
consolewrite("$tempA[0] = "&$tempA[0]&", $tempA[1] = "&$tempA[1]&@CRLF)

Revise

Share this post


Link to post
Share on other sites

Language features change over time or get extended.
So after 6 years I'm sure a lot of things which weren't possbile in 2009 can now be done.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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