Jump to content

Array Crop Columns


Recommended Posts

hello world :)

i am using this example to work off

it works great ONLY when column 0 is part of $ColumnNumbersToKeep but get this error if i add another column

Quote

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Here is the working/not working sample

#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"
$ColumnNumbersToKeep = "1,6"

$test = ArrayCropColumns($avArray, $ColumnNumbersToKeep)
Debug($test)


Func ArrayCropColumns($array, $columns)

    $aColumnsToRetain = StringSplit($columns, ",", 2)

    _ArraySort($aColumnsToRetain, 1)

    $counter = 0

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

    Return $array

EndFunc   ;==>ArrayCropColumns

Seems to crash when there are 3 columns left - cant seem to delete the next column

Any ideas?

Link to comment
Share on other sites

Try changing the following line:

$counter += 1

to:

If $counter < UBound($aColumnsToRetain) - 1 Then $counter += 1

 

$counter is being used as an array index.  Since you are manually incrementing that index inside your loop, you want to make sure that you don't increment that index if it will exceed the bounds of the array.  Which will then, given the logic of the function, delete all remaining columns (which is what you want).

Edited by TheXman
Added explanation
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...