algiuxas

Assign Array?

6 posts in this topic

#1 ·  Posted (edited)

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



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

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

This works:

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

 

Share this post


Link to post
Share on other sites

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

    • 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
    • SchneiMi
      By SchneiMi
      Hello,
      following my previous question, I have moved all potentially instable object interactions into Executes. But it Looks like "="-assignments to object Attributes cannot be done with Execute, only method calls. Using an "$obj = 1" construct, it compares (Eval) instead of sets (Execute) the value.
      I have tested multiple different combinations, using Execute and Assign, but it seems not to work with object Attributes. :-(
      Gives following Output:
      The assign Action using apply (a3) Fails, while assigning it directly, without Assign() works fine.
      The execute versions compare and do not assign, in both cases. Though, the "Execute" topic in the help file says it executes, not evaluates.
      I have found a similar, old thread, which explains this behavious but does not give a solution. https://www.autoitscript.com/forum/topic/110228-pass-object-property-as-a-variable/  
      Is there a way to assign to a com object's Attribute? Or is there anything new to this unexpected behaviour of Execute (at least compared to the help file description and Python's exec).
       
      Any help is appreciated, and thank you for all the help so far.
      Regards, Michael
       
    • MrCheese
      By MrCheese
      Hey Guys,
       
      So, the functions work, where primarygui() accurately determines the evaluation of the status of the checkboxes - the msgbox picks this up.
       
      However, later on, when we re-enter a 'for $i = 0 to ubound($checkbox)' loop, then in the 'batchinitial' function it doesn't picked up that the status&$i = 1, so it jumps out, then within the While 1 loop, it exits the loop in the first row, again because the status& $i= 0
      The "assign" line within the primarygui funtion, is this only a local assignment? if so, how can I make it cross function?
       
      Thank in adv for your help
       
       
      Func excelsheetlist() $i = 0 Global $aWorkSheets = _Excel_SheetList($oWorkbook1) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_SheetList Example 1", "Error listing Worksheets." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;_ArrayDisplay($aWorkSheets, "Excel UDF: _Excel_SheetList Example 1") ;_ArrayDisplay($aWorkSheets, "Array") Global $iRows = UBound($aWorkSheets, $UBOUND_ROWS) ; Total number of rows. In this example it will be 10. Global $iCols = UBound($aWorkSheets, $UBOUND_COLUMNS) ; Total number of columns. In this example it will be 20. Global $iDimension = UBound($aWorkSheets, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional. MsgBox($MB_SYSTEMMODAL, "", "The array is a " & $iDimension & " dimensional array with " & _ $iRows & " row(s) & " & $iCols & " column(s).") Dim $checkbox[$iRows] EndFunc ;==>excelsheetlist Func primarygui() ; Create a GUI with various controls. Local $hGUI = GUICreate("Script Controller", 300, ($iRows * 24)) ; Create a checkbox control. ;Local $idCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25) Local $Button2 = GUICtrlCreateButton("Close", 210, 200, 85, 25) Local $Button3 = GUICtrlCreateButton("Run", 210, 170, 85, 25) Local $Button1 = GUICtrlCreateButton("Discharge", 210, 140, 85, 25) For $i = 0 To UBound($checkbox) - 1 $checkbox[$i] = GUICtrlCreateCheckbox($aWorkSheets[$i][0], 8, 0 + ($i * 24)) ;, 81, 17) Next ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 $nMsg = GUIGetMsg() Select Case $nMsg = $GUI_EVENT_CLOSE Exit Case $nMsg = $Button2 ;Close Exit Case $nMsg = $Button1 MsgBox(0, "Discharge Button not configured", "Now Exiting") Exit Case $nMsg = $Button3 ;Run $fSelection = False For $i = 0 To UBound($checkbox) - 1 If BitAND(GUICtrlRead($checkbox[$i]), $GUI_CHECKED) Then $fSelection = True ExitLoop EndIf Next If $fSelection Then For $i = 0 To UBound($checkbox) - 1 Assign("status" & $i, GUICtrlRead($checkbox[$i])) Next $batchcount = 0 For $i = 0 To UBound($checkbox) - 1 If Eval("status" & $i) = 1 Then $batchcount = $batchcount + 1 ;Call ("o" & $i & "copy") ; if you want to call the functions directly, remove ; before the call and comment or delete the following DirCopy statement MsgBox(0, "Checking", "Checking that: " & $checkbox[$i] & " no, with title: " & $aWorkSheets[$i][0] & " was selected, Batch count: " & $batchcount) ; if you need only the DirCopy EndIf Next ExitLoop Else MsgBox(48, 'No Items Selected', 'You have not selected any Patients to Load, Please select from the list') EndIf EndSelect WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>primarygui Func _IsChecked($idControlID) Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED EndFunc ;==>_IsChecked Func batchinitial() If Eval("status" & $i) = 1 Then debugbox() $oWorkbook1.Sheets(1).Activate $bigloop = $bigloop + 1 $sheet = $aWorkSheets[$i][0] $oWorkbook1.Sheets($sheet).Activate debugbox() EndIf EndFunc ;==>batchinitial OpenExcel() excelsheetlist() primarygui() For $i = 0 To UBound($checkbox) - 1 batchinitial() While 1 If Eval("status" & $i) = 0 Then ExitLoop ;all the rest of my script loops etc WEnd Next  
    • Eli_jahbot
      By Eli_jahbot
      My esteemed Autoits I need your help once again.
      I'm trying to figure out how to create a loop that gets 1 value from an array and repeats until each value from the array has been used. I have never used arrays before and I know once I learn more things should get easier for me. 
      Here is what Im trying to do:
      -Have an array of values that determine what application i log into. ex: app1, app2, app3, app4 etc.
      -Have a loop that repeats a process sequentially using each value in the array to finish the process for each app1, app2, app 3 and so forth. I have 30 apps that I need to update on a regular basis and getting this sorted out is what I perceive to be the best way to do it.
      Here is my feeble attempt that obviously fails:
      #include <msgboxconstants.au3>
      #include <Constants.au3>
      #include <array.au3>
      Login()
      Func Login()
          local $array[30] = ["10", "11", "12",etc etc]
          ;;Local $site = InputBox("ERx Site","What site do you want to login as?","","")
          Local $userid = InputBox("ERx Login", "What is your username?", "", "")
          Local $Passwd = InputBox("Security Check", "Enter your UAT password.", "", "*")
      for $1 = 1 to 30(I need to do the same steps in 30 different apps)
      run("Z:launch.exe")
      WinWaitActive("Input")
      Send (Sequential ARRAY VALUE HERE)
      Send("{ENTER}")
      WinWaitActive("window")
      Send($userid)
      Send("{TAB}")
      send($Passwd)
      Send("{ENTER}")
      WinWaitActive("[CLASS:SunAwtDialog]")
      Sleep(500)
      WinClose("Home Page")
      Next
      EndFunc
       
      your help is greatly appreciated.
      Thanks for your time