Jump to content
Sign in to follow this  

2d to 2d

Recommended Posts


Hi all,

I need a hint. I'm not an adept coder by any means. I have a 2d array, and I want to copy records from the array to another 2d array. So for example, I can check strings inside the array and match them, and then write the whole row from the array to another array.

So for array:

Global $DeviceLocationList[1][4]

I already can parse through it and check against an entry in a column, so I know the row number, but I can't figure out how to copy the entire row to a new entry in a different array in the same For loop.

I am using __arrayadd for 2d arrays as downloaded from the forums to build the array. Below is a snippet where I build a 2d array, for example I would parse through that an might want to copy the record to another location. Apparently I'm too thick to understand how to read the entire row and pass it to a new array. :-(

I'm looking at this page and still scratching my head -> '?do=embed' frameborder='0' data-embedContent>>

(I use the below to get NIC card infos and then do <stuff> to them.)

Func _GetNICHWKey($PNPFilter, $ENUMKey)
    For $i = 1 To 65535
        $TopKey = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $ENUMKey, $i)
        For $j = 1 To 65535
            $SubKey = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $ENUMKey & $TopKey, $j)
            If StringInStr($SubKey, $PNPFilter) Then
                For $k = 1 To 65535
                    $DeviceKey = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $ENUMKey & $TopKey & "\" & $SubKey, $k)
                    $DeviceSubKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $ENUMKey & $TopKey & "\" & $SubKey & "\" & $DeviceKey
                    If $DeviceKey = "" Then ExitLoop

                    $DevName1 = RegRead($DeviceSubKey, "DeviceDesc")
                    $DevName2 = StringSplit($DevName1, ";")
                    $DevNameLoc = $DevName2[0]
                    $NICENUMDevDesc = $DevName2[$DevNameLoc]

                    $NICENUMDevLoc = RegRead($DeviceSubKey, "LocationInformation")
                    $DevLoc1 = StringSplit($NICENUMDevLoc, ";")
                    $DevLoc2 = $DevLoc1[0]
                    $DevLoc3 = $DevLoc1[$DevLoc2]
                    $DevLoc = $DevLoc3

                    Local $DEVLocadd[4] = [$NICENUMDevDesc, $DevLoc, "", $DeviceKey]
                    __ArrayAdd($DeviceLocationList, $DEVLocadd, False)
                    If @error <> 0 Then ExitLoop
            If $SubKey = "" Then ExitLoop
            If @error <> 0 Then ExitLoop
        If $TopKey = "" Then ExitLoop
        If @error <> 0 Then ExitLoop
    $DeviceLocationList[0][0] = UBound($DeviceLocationList)
    _ArraySort($DeviceLocationList, 0, 1, 0, 1); we sort the list of devices according to the index numbers, they end up in order of PCIbus, device, then port
    For $i = 1 To $DeviceLocationList[0][0] - 1 ; now we reference the location and deduce/insert the port number to position nr 2 by the ranking
        $DeviceLocationList[$i][2] = $i
EndFunc   ;==>_GetNICHWKey

This is the __arrayadd function used to build the array.

; #FUNCTION# ====================================================================================================================
; Name...........: __ArrayAdd
; Description ...: Adds a specified value or row at the end of an existing 1D or 2D array.
; Syntax.........: __ArrayAdd(ByRef $avArray, $vValue [, $NestArray = True])
; Parameters ....: $avArray - Array to modify ByRef
;                 $vValue  - Value to add, can be a 1D array if $avArray = 2D and $NestArray = False
;                 $NestArray - Optional flag if False causes array passed as $vValue to be interpreted as a 1D array
;                              of values to place in a single new row of a 2D array.  Default = True, saves array as
;                              a single element.  This flag is ignored if $avArray is 1D or $vValue is not an array.
; Return values .: Success - Index of last added item
;                 Failure - -1, sets @error to 1, and sets @extended to specify failure (see code below)
; Author ........: Jos van der Zande <jdeb at autoitscript dot com>
; Modified.......: Ultima - code cleanup
;             ; PsaltyDS - Array and 2D $vValue inputs added
; Remarks .......: Each call to this function adds exactly one new row to $avArray.  To add more rows use _ArrayConcatenate.
; Related .......: _ArrayConcatenate, _ArrayDelete, _ArrayInsert, _ArrayPop, _ArrayPush
; Link ..........;
; Example .......; Yes
; =================================================================================================================================
Func __ArrayAdd(ByRef $avArray, $vValue, $NestArray = True)
    Local $iBoundArray0, $iBoundArray1, $iBoundArray2, $iBoundValue1

    If IsArray($avArray) = 0 Then Return SetError(1, 0, -1); $avArray is not an array

    $iBoundArray0 = UBound($avArray, 0); No. of dimesions in array
    If $iBoundArray0 > 2 Then Return SetError(1, 1, -1); $avArray is more than 2D

    $iBoundArray1 = UBound($avArray, 1); Size of array in first dimension
    If $iBoundArray0 = 2 Then $iBoundArray2 = UBound($avArray, 2); Size of array in second dimension

    If ($iBoundArray0 = 1) Or (IsArray($vValue) = 0) Or $NestArray Then
        ; If input array is 1D, or $vValue is not an array, or $NestArray = True (default) then save $vValue literally
        If $iBoundArray0 = 1 Then
            ; Add to 1D array
            ReDim $avArray[$iBoundArray1 + 1]
            $avArray[$iBoundArray1] = $vValue
            ; Add to 2D array at [n][0]
            ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
            $avArray[$iBoundArray1][0] = $vValue
        ; If input array is 2D, and $vValue is an array, and $NestArray = False,
        ;   then $vValue is a 1D array of values to add as a new row.
        If UBound($vValue, 0) <> 1 Then Return SetError(1, 2, -1); $vValue array is not 1D
        $iBoundValue1 = UBound($vValue, 1)
        If $iBoundArray2 < $iBoundValue1 Then Return SetError(1, 3, -1); $vValue array has too many elements
        ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
        For $n = 0 To $iBoundValue1 - 1
            $avArray[$iBoundArray1][$n] = $vValue[$n]
    ; Return index of new last row in $avArray
    Return $iBoundArray1

Always carry a towel.

Share this post

Link to post
Share on other sites

Without looking, have you tried the following?

Local $aArray2 = $aArray1

This copies the first array exactly.

Edited by czardas

Share this post

Link to post
Share on other sites

You can't copy a complete row with a single command. You have to do it column by column. Example:

Global $aFrom[2][3]
Global $aTo[1][3]
; Copy row 2 of the source array to row 1 of the target array
For $i = 0 to UBound($aFrom, 2) - 1
    $aTo[1][$i] = $aFrom[2][$i]

My UDFs and Tutorials:


Active Directory (NEW 2018-12-03 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

ADO - Wiki


Share this post

Link to post
Share on other sites

Yes Water has the answer. Make the arrays the same size and then use Redim to remove unused rows after you have selected the ones you want. You will need to keep count of how many rows you have added as you go.

Edited by czardas

Share this post

Link to post
Share on other sites

What about just looping through the array (backwards) and use _ArrayDelete to kill ones you don't want?

Otherwise you'd need to loop through the row when you decide you want to keep it like water suggested.

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
Sign in to follow this  

  • Similar Content

    • lattey
      By lattey
      i have checkboxes and each checkbox that checked, i put in array. 
      now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 
      below is the code:
      #include <GUIConstantsEx.au3> ;~ #include <MsgBoxConstants.au3> #include <ButtonConstants.au3> #include <Array.au3> Global $Count = 3 Global $CheckBoxP[$Count] Global $step[$Count] global $array1[1] Global $ExitResult $hGUI = GUICreate("Summary Steps", 500, 400) GUISetFont(12, 400, "Tahoma") GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20) GUISetFont(10, 400, "Tahoma") Global $array_Pstep[3] = ["fix2","fix1","fix3"] global $step[3] = ["2","3","4"] $Spacing = 50 For $i = 0 To UBound($array_Pstep) - 1 $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17) Next $submit = GUICtrlCreateButton("Submit",180, 280, 80, 30) $exit = GUICtrlCreateButton("Exit",180, 320, 80, 30) GUISetState() While 1 $Msg = GUIGetMsg() Select case $Msg=$submit For $i = 0 To $Count - 1 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then _ArrayAdd($array1, $step[$i]) EndIf Next Global $logfilerray = @WorkingDir & "\checkedlist.txt" FileDelete ($logfilerray) Global $readlogfile = FileOpen($logfilerray,1) for $a = 1 to UBound($array1) - 1 ;~ $var=$array1[$a] FileWriteLine($readlogfile,$array1[$a]) Next FileClose($readlogfile) Exit case $Msg=$exit $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?") if $ExitResult = 1 Then ;ok Exit EndIf Exit EndSelect WEnd  
    • omicron
      By omicron
      How do you perform a nested loop function with a multidimensional array from 2 lists.
      for i in list1
      (open file) extract variable
          while open for i in list 2
          (open file2) extract variable
      var1 + var2 = (search term)

      The list sizes will more than likely consist of different lengths.
      What is the best approach to accomplishing this method?
    • omicron
      By omicron

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
      Thank you for your help and support!
    • Epic007
      By Epic007
      Hey guys I'm stuck with a program.....I need a make an automation which copies text from a chat bot app and paste it in notepad to log the replies of my chat bot and to analyse stuff....a lil help pls
      Thanks in advance
    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next