foggw Posted June 12, 2009 Share Posted June 12, 2009 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 More sharing options...
oMBRa Posted June 12, 2009 Share Posted June 12, 2009 from the help file: Assign ( "varname", "data" [, flag] ) Parameters varname The name of the variable you wish to assign. Cannot be an array element. ... Link to comment Share on other sites More sharing options...
foggw Posted June 12, 2009 Author Share Posted June 12, 2009 from the help file: ...Cannot be an array element.Thank you.Too much caffeine, not enough sleep. Link to comment Share on other sites More sharing options...
PsaltyDS Posted June 15, 2009 Share Posted June 15, 2009 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 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 More sharing options...
ludvic Posted December 12, 2009 Share Posted December 12, 2009 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 More sharing options...
ddddddclash Posted November 18, 2015 Share Posted November 18, 2015 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 More sharing options...
water Posted November 18, 2015 Share Posted November 18, 2015 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Toya Posted September 22, 2017 Share Posted September 22, 2017 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 More sharing options...
Developers Jos Posted September 22, 2017 Developers Share Posted September 22, 2017 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 More sharing options...
jchd Posted September 22, 2017 Share Posted September 22, 2017 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Toya Posted September 22, 2017 Share Posted September 22, 2017 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 More sharing options...
Developers Jos Posted September 22, 2017 Developers Share Posted September 22, 2017 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 More sharing options...
Toya Posted September 29, 2017 Share Posted September 29, 2017 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now