Sign in to follow this  
Followers 0
bourny

Deleting specific columns in a 2d Array

3 posts in this topic

I am attempting to delete specific columns in a 2d Array but keep getting the returned array with columns that I did not want to retain.

I know this is due to the Array columns changing column numbers as I delete them but I cannot get my head around how to fix this issue.

In the attached code I simply want to retain an array which only contains the columns numbered 0,1 and 6

Need to keep
        - Column 0 containing Bob and Lucy,
        - Column 1 containing Tom and mick
        - Column 6 containing Tim and janet

any ideas on how to do this .  My failing attempt below

 

include <Array.au3>



Local $avArray[5][10] = [ _
        ["BOB", "Tom", "Dave", "Jane", "Pat", "Billy", "Tim", "Paul", "Mark", "Barry"], _
        ["Lucy", "Mick", "Gary", "Sean", "Claire", "Peter", "janet", "Ken", "Ness", "Ida"]]


_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")




$ColumnNumbersToKeep= "0,1,6"  ;Keep the 1, 2 and 7 value columns

    #cs
    Need to keep
        - column 0 containing Bob and Lucy,
        - Column 1 containing Tom and mick
        - Comumn 6 containing Tim and janet

    #ce


Local $aColumnsToDelete[0]

;Reshape the CSV to remove all columns other than the ones requiring the import
Local $iCols = UBound($avArray, $UBOUND_COLUMNS)
$aColumnsToRetain = Stringsplit($ColumnNumbersToKeep, ",")
$aColumnsToRetain[0] = "Junk Value" ;Added to stop the ubound value being matched
_ArrayDisplay ($aColumnsToRetain)


    $counter = 0
    Do
        $Check = _ArraySearch($aColumnsToRetain, $counter)
        If $Check = -1 Then ;not found the column number in the array of columns to retain. = Delete the column number from the main array
            msgbox(0, "not Matched Column Number - Deleting Column", $counter)

            _ArrayColDelete($avArray, $counter)
        Else
            msgbox(0, "Matched Column Number - Keeping Column", $counter)

        EndIf

        $counter = $counter + 1

    Until $counter = $iCols

    _ArrayDisplay($avArray)

 

Share this post


Link to post
Share on other sites



#include <Array.au3>

Local $avArray[5][10] = [ _
        ["BOB", "Tom", "Dave", "Jane", "Pat", "Billy", "Tim", "Paul", "Mark", "Barry"], _
        ["Lucy", "Mick", "Gary", "Sean", "Claire", "Peter", "janet", "Ken", "Ness", "Ida"]]


_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")

$ColumnNumbersToKeep = "0,1,6" ;Keep the 1, 2 and 7 value columns

;Reshape the CSV to remove all columns other than the ones requiring the import
Local $iCols = UBound($avArray, $UBOUND_COLUMNS)
$aColumnsToRetain = StringSplit($ColumnNumbersToKeep, ",", 2) ; ########################
;$aColumnsToRetain[0] = "Junk Value" ;Added to stop the ubound value being matched
_ArraySort($aColumnsToRetain, 1) ;######################################################

$counter = 0
For $i = UBound($avArray, 2) - 1 To 0 Step -1
    If $i <> $aColumnsToRetain[$counter] Then
        _ArrayColDelete($avArray, $i)
    Else
        $counter += 1

    EndIf
Next

_ArrayDisplay($avArray)

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Perfect.

I am trying to work out how your code works to achieve this.  (Nice tidy on my array ubound value containing data.  never noticed this option)

Thanks

 

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  
Followers 0