Jump to content

Recommended Posts

Posted (edited)

My solution is to write nested arrays without copying.

The problem was described hier.

 

Function:

#include <Array.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayNestedSet
; Description ...: Assigns a value to an element of a nested 1D array.
; Syntax ........: _ArrayNestedSet(ByRef $aArray, $vIndex, $vValue)
; Parameters ....: $aArray              - an array of arrays.
;                  $vIndex              - an index or 1d-array of indexes;
;                                         a size if $vValue not defined (zero to delete).
;                  $vValue              - a value (create, resize or delete if not defined).
;
; Return values .: on success           - 1
;                    @extended            - nesting level of operation
;                  on failure           - 0
;                    @extended            - nesting level of error
;                    @error = 1           - invalid array
;                    @error = 2           - invalid index
; Author ........:
; Modified ......: kovlad
; Remarks .......: 
; Related .......:
; Link ..........: https://www.autoitscript.com/forum/topic/185638-assign-a-value-to-an-array-in-array-element/
;                  https://www.autoitscript.com/trac/autoit/ticket/3515?replyto=description
; Example .......: Yes
; ===============================================================================================================================
Func _ArrayNestedSet(ByRef $aArray, $vIndex, $vValue = Default)

    Local $extended = @extended + 1

    If IsArray($vIndex) Then
        If UBound($vIndex, 0) <> 1 Then _
            Return SetError(2, $extended)

        If UBound($vIndex) > 1 Then
            If UBound($aArray, 0) <> 1 Then _
                Return SetError(1, $extended)

            ; keep index for this array
            Local $i = $vIndex[0]
            If $i < 0 Or UBound($aArray) <= $i Then _
                Return SetError(2, $extended)

            ; delete index of this array
            _ArrayDelete($vIndex, 0)

            ; recursive function call
            Local $return = _ArrayNestedSet($aArray[$i], $vIndex, $vValue)
            If @error Then
                Return SetError(@error, @extended + 1, 0)
            Else
                Return SetExtended(@extended + 1, 1)
            EndIf
        Else
            $vIndex = $vIndex[0]
        EndIf
    EndIf

    If $vValue = Default Then
        If $vIndex < 0 Then _
            Return SetError(2, $extended)

        If $vIndex = 0 Then

            ; delete array and free memory
            $aArray = 0
            Return SetExtended($extended, 1)
        EndIf

        If UBound($aArray, 0) = 1 Then

            ; resize array keeping data
            ReDim $aArray[$vIndex]
            Return SetExtended($extended, 1)
        Else

            ; create new nested array
            Local $aTmp[$vIndex]
            $aArray = $aTmp
            Return SetExtended($extended, 1)
        EndIf
    Else
        If UBound($aArray) <= $vIndex Then _
            Return SetError(2, $extended + 1)

        ; set value of array entry
        $aArray[$vIndex] = $vValue
        Return SetExtended($extended, 1)
    EndIf

EndFunc

 

Examples:

; write value to 1st nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : write value to 1st nested array" & @CRLF)
Local $aTmp1[4] = [1,2,3,4]
_ArrayDisplay($aTmp1, "$aTmp1")
Local $aArray[2] = [$aTmp1]
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], 3, 14) = " & _ArrayNestedSet($aArray[0], 3, 14) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF & @CRLF)
_ArrayDisplay($aArray[0], "$aArray[0]")

; resize 1st nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : resize 1st nested array" & @CRLF)
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], 8) = " & _ArrayNestedSet($aArray[0], 8) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF & @CRLF)
_ArrayDisplay($aArray[0], "$aArray[0]")

; write array to 1st nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : write array to 1st nested array" & @CRLF)
Local $aTmp11[4] = [11,12,13,14]
_ArrayDisplay($aTmp11, "$aTmp11")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], 2, $aTmp11) = " & _ArrayNestedSet($aArray[0], 2, $aTmp11) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF & @CRLF)
_ArrayDisplay(($aArray[0])[2], "($aArray[0])[2]")

; write value to 2nd nested array using index array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : write value to 2nd nested array using index array" & @CRLF)
Local $aIndex1[2] = [2,3]
_ArrayDisplay($aIndex1, "$aIndex1")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], $aIndex1, 140) = " & _ArrayNestedSet($aArray[0], $aIndex1, 140) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF & @CRLF)
_ArrayDisplay(($aArray[0])[2], "($aArray[0])[2]")

; resize 2nd nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : resize 2nd nested array" & @CRLF)
Local $aIndex1[2] = [2,8]
_ArrayDisplay($aIndex1, "$aIndex1")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], $aIndex1) = " & _ArrayNestedSet($aArray[0], $aIndex1) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF & @CRLF)
_ArrayDisplay(($aArray[0])[2], "($aArray[0])[2]")

; create new 3rd nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : create new 3rd nested array" & @CRLF)
Local $aIndex2[3] = [2,7,6]
_ArrayDisplay($aIndex2, "$aIndex2")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], $aIndex2) = " & _ArrayNestedSet($aArray[0], $aIndex2) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF & @CRLF)
_ArrayDisplay((($aArray[0])[2])[7], ")($aArray[0])[2])[7]")

; delete 3rd nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : delete 3rd nested array" & @CRLF)
Local $aIndex3[3] = [2,7,0]
_ArrayDisplay($aIndex3, "$aIndex2")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], $aIndex3) = " & _ArrayNestedSet($aArray[0], $aIndex3) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF)
ConsoleWrite("IsArray((($aArray[0])[2])[7]) = " & IsArray((($aArray[0])[2])[7]) & @CRLF & @CRLF)

; write 0 in 1st nested array to delete the 2nd nested array
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : write 0 in 1st nested array to delete the 2nd nested array" & @CRLF)
Local $aIndex4[1] = [2]
_ArrayDisplay($aIndex4, "$aIndex4")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], $aIndex4, 0) = " & _ArrayNestedSet($aArray[0], $aIndex4, 0) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF)
ConsoleWrite("IsArray(($aArray[0])[2]) = " & IsArray(($aArray[0])[2]) & @CRLF & @CRLF)

; delete 1st nested array (same as '$aArray[0] = 0')
ConsoleWrite("@@ Debug(" & @ScriptLineNumber & ") : delete 1st nested array (same as '$aArray[0] = 0')" & @CRLF)
Local $aIndex5[1] = [0]
_ArrayDisplay($aIndex5, "$aIndex5")
ConsoleWrite( _
    "_ArrayNestedSet($aArray[0], $aIndex5) = " & _ArrayNestedSet($aArray[0], $aIndex5) & @CRLF & _
    "    @error    = " & @error & @CRLF & _
    "    @extended = " & @extended & @CRLF)
ConsoleWrite("IsArray($aArray[0]) = " & IsArray($aArray[0]) & @CRLF & @CRLF)

 

Edited by kovlad
Added the function description to the header.

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By DannyJ
      I have a dataset like this, (a strubg)
      Username: User1 Type: Admin RegDate: 1999 Username: User2 Type: User RegDate: 2000 How to make a 2 dimensional array that I can display with _ArrayDisplay?
      This would be a perfect 2D array to represent my data:
      Username           Tpye RegDate User1              Admin 1999 User2              User 2000   If you run this Powershell this powershell command, you can get this dataset that I am talking about:
      Get-LocalUser | Select * With this code you can try it to read into a string:
      #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include "GUIListViewEx.au3" #include <Array.au3> ; Just for display in example #RequireAdmin #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion $sCommand = "powershell.exe Get-LocalUser | Select *" Local $iPid = Run($sCommand, @WorkingDir , @SW_SHOW , $STDOUT_CHILD) ProcessWaitClose($iPid) Local $sOutput = StdoutRead($iPID) ConsoleWrite($sOutput) How can I correctly split $sOutput into a 2D array (with the above mentioned layout) that I can display and I work with?
    • By DannyJ
      $sCommands1 = 'powershell.exe Get-ChildItem' $iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2) $sOutput = ""  While 1     $sOutput &= StdoutRead($iPID)         If @error Then             ExitLoop         EndIf  WEnd ;~ msgbox(0, '' , $sOutput) ConsoleWrite("$sOutput") ConsoleWrite($sOutput) ConsoleWrite(@CRLF) $aOutput = stringsplit($sOutput ,@LF , 2) For $i=0 To  UBound($aOutput) - 1 Step 1     ConsoleWrite($aOutput[$i]) Next The script above reads the whole directory into a one dimensional array, but I need to work with the array, so I need to split the array into multiple dimensions.
      I have already read some forum answers here, and I have already tried these commands:
       
      Are there any way to use the $aOutput variable like in PowerShell:
      PowerShell:
      $a = Get-ChildItem $a.Mode I imagine this in AutoIt  $aOutput
      ConsoleWrite($aOutput[i].Mode) Or if I split this command into 2 dimension like:
      For $i To UBound($aOutput)-1 Step 1 ConsoleWrite($aOutput[$i][1]) ConsoleWrite($aOutput[$i][2]) Next  
    • By Mannyfresh31
      Please help with this 3D Array the first example works the secound doesn't.
      Need help to understand how Arrays work.
      Many thanks in advance
      ;First Example Dim $aArray[2][2][2] $aArray[0][0][0] = 1 $aArray[0][0][1] = 2 $aArray[0][1][0] = 3 $aArray[0][1][1] = 4 $aArray[1][0][0] = 5 $aArray[1][0][1] = 6 $aArray[1][1][0] = 7 $aArray[1][1][1] = 8 For $a = 0 to 1 for $b = 0 to 1 for $c = 0 to 1 ConsoleWrite($aArray[$a][$b][$c] & @CRLF) Next Next Next ;Secound Example Local $aArraym [2][2][2]=[[[1,2,],[3,4],[5,6],[7,8]]] For $a = 0 to 1 for $b = 0 to 1 for $c = 0 to 1 ConsoleWrite($aArraym[$a][$b][$c] & @CRLF) Next Next Next  
    • By Hermes
      My _Excel_RangeCopyPaste is not working as intended. What I am trying to accomplish is copy the range B:E using _Excel_RangeCopyPaste in the first row and repeat the same for row 2 and so on.
      ;Skip from reading header columns Local $Skipline = 0 ;0==> first line Local $temprf For $i = 0 To UBound($aArray2) - 1 If $Skipline = $i Then ContinueLoop $temprf &= $aArray2[$i] _Excel_RangeCopyPaste($oWorkbook.Activesheet, $oRange) Local $oTest = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "PasteButton") ;This button will paste values from the Clipboard once clicked _WD_ElementAction($sSession, $oTest, 'click')) Next Here's the full code:
      #Include <Chrome.au3> #Include <wd_core.au3> #Include <wd_helper.au3> #Include <WinHttp.au3> #include <MsgBoxConstants.au3> #include <IE.au3> #include <Array.au3> #include <Excel.au3> Local $sDesiredCapabilities, $sSession SetupChrome() _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_LoadWait($sSession) _WD_Navigate($sSession, "test.html") Local $oAppl = _Excel_Open() Local $sWorkbook = "test.xlsx" Local $oWorkbook = _Excel_BookOpen($oAppl, $sWorkbook) ;open excel and pass both parameters Local $aArray2 = _Excel_RangeRead($oWorkbook,Default,$oWorkbook.ActiveSheet.Usedrange.Columns("A:A")) Local $oRange = $oWorkbook.ActiveSheet.Range("B:E") ;Skip from reading header columns Local $Skipline = 0 ;0==> first line Local $temprf For $i = 0 To UBound($aArray2) - 1 If $Skipline = $i Then ContinueLoop $temprf &= $aArray2[$i] _Excel_RangeCopyPaste($oWorkbook.Activesheet, $oRange) Local $oTest = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "PasteButton") ;This button will paste values from the Clipboard once clicked _WD_ElementAction($sSession, $oTest, 'click')) Next _WD_Shutdown() Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') Return '{"capabilities":{"alwaysMatch":{"goog:chromeOptions":{"w3c":true,' & _ '"excludeSwitches":["enable-automation"],"useAutomationExtension":false}}}}' EndFunc ;==>SetupChrome For the first row I am trying to copy just B:E with the following info
      Apple Banana Orange Mango and then repeat for row 2, row 3, etc. I've attached the spreadhseet.
      test.xlsxI have also attached the excel file for reference.
    • By Hermes
      I have an html table that displays data along with an excel spreadsheet that has the same data as the html table. I am wanting to only match the Title column in my html table with the Title column in my Excel spreadsheet. If the titles match, click on the Edit hyperlink and continue to loop to next row. The issue I'm experience is its not matching correctly. So far  i've written the codes below:
      <table border="1" class="test"> <tr> <th> UniqueID</th> <th> Title</th> <th> UserID</th> <th> Address</th> <th> Gender </th> </tr> <tr> <td> 1 </td> <td> Title1 </td> <td> 12345 </td> <td> Manila </td> <td> <span> Male </span> </td> </tr> <tr> <td align="center" colspan="5"> <a href="#" class="testlink">Edit</a> </td> </tr> <tr> <td> 2 </td> <td> Title2 </td> <td> 67891 </td> <td> Valenzuela </td> <td> <span> Female </span> </td> </tr> <tr> <td align="center" colspan="5" > <a href="#" class="testlink">Edit</a> </td> </tr> <tr> <td> 3 </td> <td> Title3 </td> <td> 88888 </td> <td> Ohio </td> <td> <span> Male </span> </td> </tr> <tr> <td align="center" colspan="5" > <a href="#" class="testlink">Edit</a> </td> </tr> <tr> <td> 4 </td> <td> Title4 </td> <td> 77777 </td> <td> California </td> <td> <span> Female </span> </td> </tr> <tr> <td align="center" colspan="5" > <a href="#" class="testlink">Edit</a> </td> </tr> <tr> <td> 5 </td> <td> Title5 </td> <td> 33333 </td> <td> Arizona </td> <td> <span> Male </span> </td> </tr> <tr> <td align="center" colspan="5" > <a href="#" class="testlink">Edit</a> </td> </tr> </table> #Include "Chrome.au3" #Include "wd_core.au3" #Include "wd_helper.au3" #Include "Excel.au3" #Include "_HtmlTable2Array.au3" #Include "Array.au3" Local $sDesiredCapabilities, $sSession SetupChrome() _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_LoadWait($sSession) _WD_Navigate($sSession, "index.html") Sleep(6000) Local $oExcel = _Excel_Open() Local $oWorkbook = _Excel_BookOpen($oExcel, "test.xlsx") ; Get the table element $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//table[@class='test']") ; Retrieve HTML $sHTML = _WD_ElementAction($sSession, $sElement, "Property", "outerHTML") ;Local $aTable = _HtmlTableGetWriteToArray($sHTML) Local $aArray1 = _Excel_RangeRead($oWorkbook,1,$oWorkbook.ActiveSheet.Usedrange.Columns("B:B")) Local $aArray2 = _HtmlTableGetWriteToArray($sHTML) ;_ArrayDisplay($aArray1) ;_ArrayDisplay($aArray2) For $i = UBound($aArray1) - 1 To 0 step - 1 For $j = UBound($aArray2) - 1 to 0 step - 1 If $aArray1[$i][1] == $aArray2[$j][1] Then _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//a[contains(@class,'testlink') or contains(text(),'Edit')]") $test1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a[contains(@class,'testlink') or contains(text(),'Edit')]") _WD_ElementAction($sSession, $test1, 'click') ;_ArrayDisplay($aArray1) ;_ArrayDelete($aArray1 , $i) ;exitloop EndIf Next Next _WD_Shutdown() Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}' EndFunc ;==>SetupChrome Would appreciate if anyone can provide tips, or point me in the right direction in doing it.
       
      test.xlsx
×
×
  • Create New...