Jump to content

Unable to Delete Multiple Rows with _ArrayDelete


Recommended Posts

Greetings,

Using _ArrayDelete, I have not been successful trying to delete multiple rows that contain the same value. The value is located in the same 2D array column. I receive the error (427) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Referring to the script below, _ArrayDisplay($aArray) does return all of the data from the "MyData.csv" file. The value "28" is always located in $aArray's Col0. The number of rows in the csv file changes daily and the number of columns remains the same. ArrayDisplay($vRange) returns all of the row numbers that contain "28", and lists these row numbers in a single Col0 (please see attached screenshot). The help file's Remarks for _ArrayDelete state: "$vRange can also be a 1D array listing all rows to be deleted with the count in the [0] element]." My (very new to scripting) interpritation of this remark is that $vRange fulfills the requirements. I searched this forum and the Internet, and didn't find anything I thought was useful. I hope someone can show me the error of my ways.

Thank you!

#include <Array.au3>
#include <File.au3>
#include <CSV.au3>
#include <String.au3>
#include <SFTPEx.au3>
#include <MsgBoxConstants.au3>

$path = @ScriptDir&"\"

$aArray=_ParseCSV($path&"MyData.csv")
_ArrayDisplay($aArray)

$vRange=_ArrayFindAll($aArray, "28", Default, Default, Default, Default, 0)
_ArrayDisplay ($vRange)

_ArrayDelete($aArray, $vRange)

 

_ArrayDisplay$vRange.png

Edited by BasementDweller
Link to comment
Share on other sites

  • Moderators

BasementDweller,

_ArrayFindAll returns an array - _ArrayDelete requires a delimited string as a range. Therein lies your problem.

Convert the array into string using _ArrayToString and setting the $sDelim_Col parameter to ";" will give you a formatted string that _ArrayDelete will accept:

#include <Array.au3>

Global $aArray[20]

For $i = 0 To 19
    $aArray[$i] = Random(1, 5, 1)
Next
_ArrayDisplay($aArray)

$vRange =_ArrayFindAll($aArray, "2", Default, Default, Default, Default, 0)
_ArrayDisplay ($vRange)

$vRange = _ArrayToString($vRange, ";")
ConsoleWrite($vRange & @CRLF)

_ArrayDelete($aArray, $vRange)
_ArrayDisplay($aArray)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I do a similar process with a daily csv file.  I have to delete rows that contain different numbers.     For example I need to delete every row that contains the number 72275.   I use a function that I found on this forum.   Here is the code:

Global $aArray
_FileReadToArray ( 'Pathtoyourfile\Yourfilename.csv', $aArray )
$aArray = _DeleteArrayElementWithStringInstr ( $aArray, '72275' )   ; The 72275 can be any string to search for
_FileWriteFromArray ( 'Pathtoyourfile\Yourfilename.csv', $aArray, 1 )

;The funtion
Func _DeleteArrayElementWithStringInstr ( $aArray, $_String )
    Local $_Item
    For $_Element In $aArray
        If StringInStr ( $_Element, $_String ) <> 0 Then
            _ArrayDelete ( $aArray, $_Item )
        Else
            $_Item+=1
        EndIf
    Next
    Return ( $aArray )
EndFunc ;==> _DeleteArrayElementWithStringInstr ( )

Just another way to do it. 

Link to comment
Share on other sites

  • Moderators

xcaliber13,

That is a dreadfully slow way to do it as you will call ReDim (one of the slowest functions in AutoIt) every time you delete a line. Using _ArrayDelete with a range only calls ReDim once per call - which is why I wrote it that way!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

M23 -- I greatly appreciate your suggestion, which worked perfectly. Additionally, I took a look at your UDF's. They are inspiring. I hope to be able to write my own UDFs some day.

And to both you and xcaliber13 -- Thank you very much for taking the time to comment!

Edited by BasementDweller
Link to comment
Share on other sites

$vRange : Element(s) to delete - either a single index, a range string or a 1D array with a count in the [0] element  

You can use an array to delete but you must provide a count at first, which is something that _ArrayFindAll doesn't provide

Link to comment
Share on other sites

  • Moderators

Nine,

Well spotted!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

22 hours ago, Nine said:

$vRange : Element(s) to delete - either a single index, a range string or a 1D array with a count in the [0] element  

You can use an array to delete but you must provide a count at first, which is something that _ArrayFindAll doesn't provide

Hi Nine -- Will you please show me what is meant by a 1D array with a count in the [0] element, compared to a 1D array without a count in the [0] element?

Link to comment
Share on other sites

  • Moderators

BasementDweller,

A 1D array with a count looks like this:

Local $aArray[] = [4, 1, 2, 3, 4]

whereas without a count it looks like this:

Local $aArray[] = [1, 2, 3, 4]

There is no functional difference between the 2 types - you can easily get the count by using UBound - so it is a matter of personal choice. Many AutoIt functions return arrays with a count in the [0] element - unfortunately for you  _ArrayFindAll does not so you would need to do something like this to convert the returned array:

#include <Array.au3>

; Simulate no-count array returned from _ArrayFindAll
Local $aArray[] = [1, 2, 3, 4]

; Add a count into the [0] element
_ArrayInsert($aArray, 0, UBound($aArray))

; And here is the array with a count
_ArrayDisplay($aArray, "", Default, 8)

So now you have 2 ways to skin this particular cat - your choice!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Link to comment
Share on other sites

22 minutes ago, BasementDweller said:

Am I correct to say that a count in the [0] element of a 1D array is a number that always refers to the total number of rows of the array?

No it is just one of the way to implement a count 

23 minutes ago, BasementDweller said:

And a followup question, please: Is there a [0] element in a 2D array?

Again no, for the same reason as above...

Link to comment
Share on other sites

  • Moderators

BasementDweller,

The [0] element is an element like all of the other elements. It is not exclusively used as a count - it can be used for anything. Hence the need to read the Help file carefully to see what exactly is in, or required to be in, a particular array with which you are dealing.

A 2D array does not have a [0] element - but does have a [0][0] element - which similarly can be used for a count, although not necessarily.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...