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

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-06-01 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version - Download - General Help & Support

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

    • TrashBoat
      By TrashBoat
      So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
      Global $map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
    • Zein
      By Zein
      #include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local $aRetArray Local $sFilePath = "n.csv" _FileReadToArray($sFilePath, $aRetArray, ",") ; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",") _ArrayDisplay($aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
      The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

      I looked again at the documentation:
      #include <File.au3> _FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )
      And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
      I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

    • ternal
      By ternal
      Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
      I did not put much efffort into error checking but so far I did not need it.
      For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
      If anyone has big lists of random stuff to sort could you try this out please?
      #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref $array[, $first_index = Default[, $second_index = Default[, $ascending = Default]]]) ; Parameters ....: $array - 2d array to sort. ; $first_index - [optional] first column to sort. Default is 0. ; $second_index - [optional] second column to sort. Default is 1. ; $ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref $array, $first_index = Default, $second_index = Default, $ascending = Default) Local $temp_value Local $counter = 1 If UBound($array, $UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If $first_index = Default Then $first_index = 0 If $second_index = Default Then $second_index = 1 If $ascending = Default Then $ascending = 1 _ArraySort($array, $ascending, 0, 0, $first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf $temp_value = $array[0][$first_index] For $x = 1 to UBound($array, 1) - 1 If Mod( $x, 10000) = 0 Then ConsoleWrite("at " & $x & " of a total : " & UBound($array, 1) & @CRLF) If $array[$x][$first_index] = $temp_value Then $counter+= 1 If $x = UBound($array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort($array, $ascending, $x - $counter, $x, $second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If $counter > 0 Then ;at least 2 of the same _ArraySort($array, $ascending, $x - $counter, $x - 1, $second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf $counter = 1 EndIf EndIf $temp_value = $array[$x][$first_index] Next Return 1 EndFunc Kind regards, Ternal
    • TrashBoat
      By TrashBoat
      So I've made this script that detects how long i have held down my left mouse button for and stores the information in an array and then sorts its using _ArraySort but the output is half sorted half broken.
      Here's my script:
      HotKeySet("{F1}","_exit") #include <Misc.au3> #include <Timers.au3> #include <Array.au3> Local $dll = DllOpen("user32.dll") $on = False Global $array[0] While(1) If _IsPressed(01,$dll) Then $timer = _Timer_Init() While _IsPressed(01,$dll) Sleep(1) WEnd $time = _Timer_Diff($timer) _ArrayAdd($array,"Time: " & Floor($time) & " ms") ;~ ConsoleWrite("Time: " & Floor($time) & " ms" & @CRLF) EndIf Sleep(50) WEnd Func _exit() _ArraySort($array) _ArrayDisplay($array) Exit EndFunc And the output:

      See how its not sorted?  What is the problem here?
    • MrCheese
      By MrCheese
      Hi guys,
      See attached for an array example.
      to simplify what i want to achieve,  I want to split this array into 9 different csv files.
      the first file would contain the list of "key" and the corresponding "ID1", the second would have "key" and the "ID2", the third would have "key" and "ID3"
      However, I want to remove all the rows that don't have an ID recorded in the respective ID2, ID3 4...5...6 etc, so the file only contains row items with a key and the ID.
      Would be the best way to loop through the rows and delete the row if the array field is blank - would I then need to repeat that row ID to check that the row that its replaced is also empty (ie the one after the one I just deleted)? I see this getting messy.
      or _arraySort, and delete everything below the last filled row? <-- this might be best?
      Or should I use the excel UDF, apply a filter (not selecting the blanks), then create/export to the array->csv?
      Super keen to hear your thoughts.