foggw

Syntax to "ASSIGN/EVAL" Array variable

13 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

Even now 8 years later, the Assign function still says you cannot use as an input an array element (like $Array[3]). In fact, if the variable name is given as a text this can be tricky to solve. I used the following. The idea is to obtain the arrayname (removing the trailing "[number]") and copying into an array variable that we know the name and therefore can modify its element position "number", to later revert to array again. Maybe not the best solution but at least now I have an Assign2 function that can work either with variables ($Var) or array elements ($Array[2])

#include <array.au3>

Example()

Func CheckIfVariableNameIsAnArray(const $ParamName)
    return (StringRegExp($ParamName, "(\[)([0-9]{1,2})(\])$"))? True : False
Endfunc
Func CheckIfVariableNameIsAnArrayAndReturnItsArrayIndex(const $ParamName)
    if StringRegExp($ParamName, "(\[)([0-9]{1,2})(\])$") Then
        local $ArrayPos=StringTrimLeft(StringTrimRight(_ArrayToString(StringRegExp($ParamName, "(\[)([0-9]{1,2})(\])$",1),""),1),1) ; This removes the "[]" covering number like: [number]
        return $ArrayPos
    Else
        return ""
    endif
Endfunc
Func Assign2(const $ParamName, const $Value)
    local $ArrayName=""
    local $ArrayPos=""
    if not CheckIfVariableNameIsAnArray($ParamName) or IsNumber($ParamName) Then
        Assign($ParamName, $Value)
    Else
        $ArrayPos=CheckIfVariableNameIsAnArrayAndReturnItsArrayIndex($ParamName)
        if $ArrayPos <> "" then
            $ArrayName=StringTrimRight($ParamName,2+StringLen($ArrayPos)) ; This removes the trailing "[number]"  like: Array[number] -> Array
            local $ArrayTmp=Eval($ArrayName)
            $ArrayTmp[$ArrayPos]=$Value
            Assign($ArrayName, $ArrayTmp)
        endif
    endif
EndFunc

func Example()
    local $Array[3]=[1,2,3]
    local $VariableElementToChange="Array[2]"
    Assign2(VariableElementToChange, "7") 
endfunc

 

Share this post


Link to post
Share on other sites
8 minutes ago, Toya said:

Even now 8 years later, the Assign function still says you cannot use as an input an array element (like $Array[3]).

... and your point is?


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Assign/Eval being pretty convoluted useless constructs, the point is not only seriously old but moot.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
32 minutes ago, Jos said:

... and your point is?

My point is that if you want to use Assign with an array element you need to do it manually or in case you do not know the variable name while developing the code you need to use a temporary array variable or use the code I shared, which shows an "Assign2()" function that works no matter what you pass a variable name or a array element name.

Share this post


Link to post
Share on other sites
On 9/22/2017 at 11:28 PM, Jos said:

That is a correct conclusion indeed, but that sentence implied something else as well. 

Jos

Thank you.

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