Jump to content
Sign in to follow this  
ModemJunki

2d to 2d

Recommended Posts

ModemJunki

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

Share this post


Link to post
Share on other sites
czardas

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
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
czardas

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
ZacUSNYR

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

    • 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  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) 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] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
×