Jump to content

Syntax to "ASSIGN/EVAL" Array variable


Recommended Posts

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

Link to comment
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
Link to comment
Share on other sites

  • 5 months later...

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.

Link to comment
Share on other sites

  • 5 years later...

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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • 1 year later...

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

 

Link to comment
Share on other sites

  • Developers
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?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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)

Link to comment
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.

Link to comment
Share on other sites

  • Developers

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

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...