Jump to content

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
                Next
            EndIf
            If $SubKey = "" Then ExitLoop
            If @error <> 0 Then ExitLoop
        Next
        If $TopKey = "" Then ExitLoop
        If @error <> 0 Then ExitLoop
    Next
    $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
    Next
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
        Else
            ; Add to 2D array at [n][0]
            ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
            $avArray[$iBoundArray1][0] = $vValue
        EndIf
    Else
        ; 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]
        Next
    EndIf
    ; Return index of new last row in $avArray
    Return $iBoundArray1

Always carry a towel.

Link to comment
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]
Next

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

×
×
  • Create New...