Jump to content
algiuxas

Assign Array?

Recommended Posts

algiuxas

Hello,

I need to assign data to array:

Global $Test[3][4]
$Array = "Test"
SetArrayData($Array,"Hello world!",1,3)
ConsoleWrite($Array[1][3]&@CRLF)
Exit
Func SetArrayData($Array,$Data,$1,$2)
    $Array[$1][$2] = $Data; <-- ???
EndFunc

I need that somebody would help me with that function :)

Edited by algiuxas

42783.png

Share this post


Link to post
Share on other sites
InunoTaishou

You're trying to assign a value to an array that is not actually an array?

You'll need to test if the variable being passed is an array or not. If it's not then create a temporary array and assign it ByRef.

#include <Array.au3>

Global $aTest1[3][4]
Global $sTest2 = "Test"
Global $aTest3[1]

SetArrayData($aTest1, "This is Row 2, Column 3", 2, 3)
If (@Error) Then SetArrayError(@Error, "$aTest1")
SetArrayData($sTest2, "This is Row 9, Column 3", 9, 3)
If (@Error) Then SetArrayError(@Error, "$sTest2")
SetArrayData($aTest3, "This is Row 2, Column 2", 2, 2, False)
If (@Error) Then SetArrayError(@Error, "$aTest3")

_ArrayDisplay($aTest1)
_ArrayDisplay($sTest2)
_ArrayDisplay($aTest3)


Func SetArrayData(ByRef $aArray, Const ByRef $sData, Const ByRef $iRow, Const ByRef $iColumn, Const $ReDim = True)
    If (Not IsArray($aArray)) Then
        ConsoleWrite("$aArray is not an array" & @LF)
        If (Not $ReDim) Then Return SetError(-1, 0, False)
        Local $aTemp[$iRow + 1][$iColumn + 1]
        $aArray = $aTemp
        ConsoleWrite("Converted $aArray to proper array" & @LF)
    EndIf

    If ($iRow >= UBound($aArray, $UBOUND_ROWS)) Then
        ConsoleWrite("Assigning to row " & $iRow & " when rows in $aArray are " & UBound($aArray, $UBOUND_ROWS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(1, 0, False)
        ReDim $aArray[$iRow + 2][$iColumn + 1]
        ConsoleWrite("Increased $aArray row count to " & UBound($aArray, $UBOUND_ROWS) & @LF)
    EndIf

    If ($iColumn >= UBound($aArray, $UBOUND_COLUMNS)) Then
        ConsoleWrite("Assigning to column " & $iColumn & " when columns in $aArray are " & UBound($aArray, $UBOUND_COLUMNS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(2, 0, False)
        ReDim $aArray[$iRow + 1][$iColumn + 2]
        ConsoleWrite("Increased $aArray column count to " & UBound($aArray, $UBOUND_COLUMNS) & @LF)
    EndIf

    $aArray[$iRow][$iColumn] = $sData
    Return True
EndFunc

Func SetArrayError($iError, $sVar)
    Switch ($iError)
        Case -1
            MsgBox("", "SetArrayData Error", $sVar & " passed to SetArrayData was not an actual Array and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Row passed to SetArrayData was out of range for the variable " & $sVar & " and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Column passed to SetArrayData was out of range for the  " & $sVar & " and $ReDim was set to false")
    EndSwitch
EndFunc

 

  • Like 1

Share this post


Link to post
Share on other sites
algiuxas
23 minutes ago, InunoTaishou said:

You're trying to assign a value to an array that is not actually an array?

You'll need to test if the variable being passed is an array or not. If it's not then create a temporary array and assign it ByRef.

#include <Array.au3>

Global $aTest1[3][4]
Global $sTest2 = "Test"
Global $aTest3[1]

SetArrayData($aTest1, "This is Row 2, Column 3", 2, 3)
If (@Error) Then SetArrayError(@Error, "$aTest1")
SetArrayData($sTest2, "This is Row 9, Column 3", 9, 3)
If (@Error) Then SetArrayError(@Error, "$sTest2")
SetArrayData($aTest3, "This is Row 2, Column 2", 2, 2, False)
If (@Error) Then SetArrayError(@Error, "$aTest3")

_ArrayDisplay($aTest1)
_ArrayDisplay($sTest2)
_ArrayDisplay($aTest3)


Func SetArrayData(ByRef $aArray, Const ByRef $sData, Const ByRef $iRow, Const ByRef $iColumn, Const $ReDim = True)
    If (Not IsArray($aArray)) Then
        ConsoleWrite("$aArray is not an array" & @LF)
        If (Not $ReDim) Then Return SetError(-1, 0, False)
        Local $aTemp[$iRow + 1][$iColumn + 1]
        $aArray = $aTemp
        ConsoleWrite("Converted $aArray to proper array" & @LF)
    EndIf

    If ($iRow >= UBound($aArray, $UBOUND_ROWS)) Then
        ConsoleWrite("Assigning to row " & $iRow & " when rows in $aArray are " & UBound($aArray, $UBOUND_ROWS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(1, 0, False)
        ReDim $aArray[$iRow + 2][$iColumn + 1]
        ConsoleWrite("Increased $aArray row count to " & UBound($aArray, $UBOUND_ROWS) & @LF)
    EndIf

    If ($iColumn >= UBound($aArray, $UBOUND_COLUMNS)) Then
        ConsoleWrite("Assigning to column " & $iColumn & " when columns in $aArray are " & UBound($aArray, $UBOUND_COLUMNS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(2, 0, False)
        ReDim $aArray[$iRow + 1][$iColumn + 2]
        ConsoleWrite("Increased $aArray column count to " & UBound($aArray, $UBOUND_COLUMNS) & @LF)
    EndIf

    $aArray[$iRow][$iColumn] = $sData
    Return True
EndFunc

Func SetArrayError($iError, $sVar)
    Switch ($iError)
        Case -1
            MsgBox("", "SetArrayData Error", $sVar & " passed to SetArrayData was not an actual Array and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Row passed to SetArrayData was out of range for the variable " & $sVar & " and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Column passed to SetArrayData was out of range for the  " & $sVar & " and $ReDim was set to false")
    EndSwitch
EndFunc

 

Thank You, but what I need to do if it's just array name? Example:

;...
Global $Array1[3][4]
Global $Array2[3][4]
Global $Array3[3][4]
;...
For $i = 1 to 3
    SetArrayData("Array"&$i,$i,1,1)
Next
;Random...
For $i = 1 to 3
    SetArrayData("Array"&Random(1,3,1),"Hello world!",Random(0,2,1),Random(0,3,1))
Next
;...

No, I'm not trying to assign a value to an array that is not actually an array.

Edited by algiuxas

42783.png

Share this post


Link to post
Share on other sites
InunoTaishou

I think this is kind of what you're trying to accomplish then? Using Assign and Eval. You can't assign it in the actual function itself because you're trying to use the variables as strings. Eval and Assign don't actually assign/eval values in arrays, they just work on the variable itself. If you use Eval you can get the whole array, but you can't use ByRef in the SetArrayData function. Instead, return an array and use Assign to do what you want.

#include <Array.au3>

Global $aTest1
Global $aTest2
Global $aTest3
Global $ReDimArray[] = [False, True, True, False]

For $i = 1 to 3
    Local $iRow = Random(0, 4, 1)
    Local $iColumn = Random(0, 4, 1)
    Assign("aTest" & $i, SetArrayData(Eval("aTest" & $i), "This is Row " & $iRow & ", Column " & $iColumn & " for array $aTest" & $i, $iRow, $iColumn))
Next

_ArrayDisplay($aTest1, "$aTest1")
_ArrayDisplay($aTest2, "$aTest2")
_ArrayDisplay($aTest3, "$aTest3")

Func SetArrayData($aArray, Const ByRef $sData, Const ByRef $iRow, Const ByRef $iColumn, Const $ReDim = True)
    If (Not IsArray($aArray)) Then
        ConsoleWrite("$aArray is not an array" & @LF)
        If (Not $ReDim) Then Return SetError(-1, 0, False)
        Local $aTemp[$iRow + 1][$iColumn + 1]
        $aArray = $aTemp
        ConsoleWrite("Converted $aArray to proper array" & @LF)
    EndIf

    If ($iRow >= UBound($aArray, $UBOUND_ROWS)) Then
        ConsoleWrite("Assigning to row " & $iRow & " when rows in $aArray are " & UBound($aArray, $UBOUND_ROWS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(1, 0, False)
        ReDim $aArray[$iRow + 2][$iColumn + 1]
        ConsoleWrite("Increased $aArray row count to " & UBound($aArray, $UBOUND_ROWS) & @LF)
    EndIf

    If ($iColumn >= UBound($aArray, $UBOUND_COLUMNS)) Then
        ConsoleWrite("Assigning to column " & $iColumn & " when columns in $aArray are " & UBound($aArray, $UBOUND_COLUMNS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(2, 0, False)
        ReDim $aArray[$iRow + 1][$iColumn + 2]
        ConsoleWrite("Increased $aArray column count to " & UBound($aArray, $UBOUND_COLUMNS) & @LF)
    EndIf

    $aArray[$iRow][$iColumn] = $sData
    Return $aArray
EndFunc

 

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

  • Similar Content

    • AnonymousX
      By AnonymousX
      Hello,
      So this may be more of a challenge of effective programming then specific to AutoIT but I want to solve this problem with AutoIT  so i'm putting it here. (If someone has a better language to solve with I'm all ears)
       
      So the task I'm trying to achieve is that I have multiple .CSV files that have: year, month, day, hour, value. I need to be able to sum up all the values that have the same date/time, then find which date and time has the maximum value.
       
      The problem is that each file may or may not have same amount of days/hours as the rest. So I need to devise a way to handle this. 
       
      Example:
      File A   File B   File C 2018 1 1  1:00 10   2018 1 1 2:00 10   2018 1 1  1:00 10 2018 1 1  2:00 12   2018 1 1 3:00 12   2018 1 2 1:00 12 2018 1 1  3:00 14   2018 1 1 4:00 14   2018 2 1  1:00 16 2018 2 1  1:00 16   2018 2 1  1:00 16              
       Answer I want to be spit out is Feb 1st 2018 at 2:00 with value of 48
       
      So far I've got code to store all .CSV files to an array, then a loop to go through each csv, but not sure how to effectively manipulate the data. Keep in mind each file has over 7000 time entry points.
       
      If anyone can solve this that would be pretty awesome! 
      #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> RefineData() Func RefineData() Local $i, $filenum, $file, $csvArray, $FilePath = @ScriptDir $fileList = _FileListToArrayRec($FilePath, "*.csv", 1) ;Create and array of all .csv files within folder Local $chkArray[UBound($fileList)][2] ;=====Loop through the .csv files within the folder====== For $filenum = 1 To UBound($fileList) - 1 Step 1 $file = $fileList[$filenum] $sFilePath = $FilePath & "\" & $file ;=====Create array based on csv file===== _FileReadToArray($sFilePath, $csvArray, $FRTA_NOCOUNT, ",") ;#### Operations here ###### next msgbox(0,"", "Date: " & $date_of_max & "Value: " & $maxVal );display solution endfunc  
    • corz
      By corz
      Associative Array Functions
      I've seen a couple of UDFs for this on the forum. One of them I quite like. But it's still nearly not as good as this method, IMHO.
      I don't recall if I discovered the "Scripting.Dictionary" COM object myself or if I got the original base code from somewhere online. I have recently searched the web (and here) hard for any AutoIt references to this, other than my own over the years I've been using this (in ffe, etc..), and I can find nothing, so I dunno. If anyone does, I'd love to give credit where it's due; this is some cute stuff! It could actually be all my own work! lol
      At any rate, it's too useful to not have posted somewhere at autoitscript.com, so I've put together a wee demo.
      For those who haven't heard of the COM "Scripting.Dictionary".. 
      If you've ever coded in Perl or PHP (and many other languages), you know how useful associative arrays are. Basically, rather than having to iterate through an array to discover it's values, with an associative array you simply pluck values out by their key "names".
      I've added a few functions over the years, tweaked and tuned, and this now represent pretty much everything you need to easily work with associative arrays in AutoIt. En-joy!
      The main selling point of this approach is its simplicity and weight. I mean, look at how much code it takes to work with associative arrays! The demo is bigger than all the functions put together! The other selling point is that we are using Windows' built-in COM object functions which are at least theoretically, fast and robust.
      I've used it many times without issues, anyhow, here goes..
      ; Associative arrays in AutoIt? Hells yeah! ; Initialize your array ... global $oMyError = ObjEvent("AutoIt.Error", "AAError") ; Initialize a COM error handler ; first example, simple. global $simple AAInit($simple) AAAdd($simple, "John", "Baptist") AAAdd($simple, "Mary", "Lady Of The Night") AAAdd($simple, "Trump", "Silly Man-Child") AAList($simple) debug("It is said that Trump is a " & AAGetItem($simple, "Trump") & ".", @ScriptLineNumber);debug debug("") ; slightly more interesting.. $ini_path = "AA_Test.ini" ; Put this prefs section in your ini file.. ; [test] ; foo=foo value ; foo2=foo2 value ; bar=bar value ; bar2=bar2 value global $associative_array AAInit($associative_array) ; We are going to convert this 2D array into a cute associative array where we ; can access the values by simply using their respective key names.. $test_array = IniReadSection($ini_path, "test") for $z = 1 to 2 ; do it twice, to show that the items are *really* there! for $i = 1 to $test_array[0][0] $key_name = $test_array[$i][0] debug("Adding '" & $key_name & "'..");debug ; key already exists in "$associative_array", use the pre-determined value.. if AAExists($associative_array, $key_name) then $this_value = AAGetItem($associative_array, $key_name) debug("key_name ALREADY EXISTS! : =>" & $key_name & "<=" , @ScriptLineNumber);debug else $this_value = $test_array[$i][1] ; store left=right value pair in AA if $this_value then AAAdd($associative_array, $key_name, $this_value) endif endif next next debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) debug(@CRLF & "Removing 'foo'..");debug AARemove($associative_array, "foo") debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) debug(@CRLF & "Removing 'bar'..");debug AARemove($associative_array, "bar") debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) quit() func quit() AAWipe($associative_array) AAWipe($simple) endfunc ;; Begin AA Functions func AAInit(ByRef $dict_obj) $dict_obj = ObjCreate("Scripting.Dictionary") endfunc ; Adds a key and item pair to a Dictionary object.. func AAAdd(ByRef $dict_obj, $key, $val) $dict_obj.Add($key, $val) If @error Then return SetError(1, 1, -1) endfunc ; Removes a key and item pair from a Dictionary object.. func AARemove(ByRef $dict_obj, $key) $dict_obj.Remove($key) If @error Then return SetError(1, 1, -1) endfunc ; Returns true if a specified key exists in the associative array, false if not.. func AAExists(ByRef $dict_obj, $key) return $dict_obj.Exists($key) endfunc ; Returns a value for a specified key name in the associative array.. func AAGetItem(ByRef $dict_obj, $key) return $dict_obj.Item($key) endfunc ; Returns the total number of keys in the array.. func AACount(ByRef $dict_obj) return $dict_obj.Count endfunc ; List all the "Key" > "Item" pairs in the array.. func AAList(ByRef $dict_obj) debug("AAList: =>", @ScriptLineNumber);debug local $k = $dict_obj.Keys ; Get the keys ; local $a = $dict_obj.Items ; Get the items for $i = 0 to AACount($dict_obj) -1 ; Iterate the array debug($k[$i] & " ==> " & AAGetItem($dict_obj, $k[$i])) next endfunc ; Wipe the array, obviously. func AAWipe(ByRef $dict_obj) $dict_obj.RemoveAll() endfunc ; Oh oh! func AAError() Local $err = $oMyError.number If $err = 0 Then $err = -1 SetError($err) ; to check for after this function returns endfunc ;; End AA Functions. ; debug() (trimmed-down version) ; ; provides quick debug report in your console.. func debug($d_string, $ln=false) local $pre ; For Jump-to-Line in Notepad++ if $ln then $pre = "(" & $ln & ") " & @Tab ConsoleWrite($pre & $d_string & @CRLF) endfunc  
      ;o) Cor
    • TheWizEd
      By TheWizEd
      How do I work with 2D arrays.  I've tried this but get errors.
      Local $aTest[4][4] = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
      ;$aTest[0][] = [10,11,12]  ; Error at []
      Local $sTest = ""
      For $i = 0 To UBound($aTest)-1
        Local $aExtract = _ArrayExtract($aTest,$i,$i)
        $sTest = $sTest & MyTest($aExtract)
      Next
      Func MyTest($aTemp)
        _ArrayDisplay($aTemp)
        ; Error at    v $aTemp
        Return String($aTemp[0]) & " - " & String($aTemp[1]) & " - " & String($aTemp[2]) & @CRLF
      EndFunc
       
       
    • kartune
      By kartune
      Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.
      Here is an example:
       
      <autoit>
      Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ
      GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
      </autoit>
      Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.
      I've already tried
      <autoit>
      Assign($clear1, $SATJ)
      </autoit>
       
      any help is greatly appreciated!!! 
    • rkr
      By rkr
      Hi, I have a software which has a 'run file'-ie; when i double click on that file, the input file of the software is run by the software. I wish to double click/execute this run file using autoit. how do i achieve this. i opened the software and it shows the input file and run file in same folder, i will put my autoitscript file too in same location. how do i simulate double clicking the run file (using its file name, say - abc.run ). I entered the following lines to make software window active - it became active, dont know how to go forward
      Winactivate("program title")
      winwaitactivate("program title")
      controlclick("program title","abc.run",1000,"primary",2) ; 1000 is ctrl id
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.