Jump to content

_ArrayDelete

Recommended Posts

I'm looking at the functions inside of Array.au3 and I came across something in _ArrayDelete that I had a question about.

The section for bounds checking will change anything less than zero or greater than the upbound of the array.  Shouldn't the function instead return an error?  If you're passing anything less than zero to this function then you should probably take a step back and evaluate your situation.

```Func _ArrayDelete(ByRef \$avArray, Const \$iElement)
If Not IsArray(\$avArray) Then Return SetError(1, 0, False)

Local Const \$iUBound = UBound(\$avArray) - 1

; Bounds checking
If \$iElement < 0 Then \$iElement = 0
If \$iElement > \$iUBound Then \$iElement = \$iUBound

; Move items after \$iElement up by 1
Switch UBound(\$avArray, 0)
Case 1
For \$i = \$iElement To \$iUBound - 1
\$avArray[\$i] = \$avArray[\$i + 1]
Next

ReDim \$avArray[\$iUBound]

Case 2
Local Const \$iSubMax = UBound(\$avArray, 2) - 1

For \$i = \$iElement To \$iUBound - 1
For \$j = 0 To \$iSubMax
\$avArray[\$i][\$j] = \$avArray[\$i + 1][\$j]
Next
Next

ReDim \$avArray[\$iUBound][\$iSubMax + 1]

Case Else
Return SetError(2, 0, False)
EndSwitch

Return \$iUBound
EndFunc   ;==>_ArrayDelete```

Share this post

Share on other sites

If less than 0 is passed to the \$iElement parameter then a mistake is assumed and 0 forced.

At that point there has definitely been an array passed anyway.

If I recall I think an AutoIt array actually has elements below zero albeit only used internally, so you don't want them deleted.

Monkey's are, like, natures humans.

Share this post

Share on other sites

I dunno, still seems weird to make that assumption.  What's this about arrays having a negative bound?

I think it still stands that if you're passing a value that is out of bounds of the array then you should reevaluate.

Share this post

Share on other sites

It's a valid point you're making without a doubt.

But The UDF wants to only be sure itself does not error out, and puts the onus on the caller to check its arguments.

Edited by JohnOne

Monkey's are, like, natures humans.

Share this post

Share on other sites

jaberwocky6669,

I think JohnOne has put it very nicely. Why depend on a UDF to detect your coding error? You have to check for an error in either case, so why not before rather than after?

Furthermore, trying to cater for every possible parameter error for every UDF would bloat the include files enormously - and given the ingenuity of coders, would probably never catch every single one. In this case setting the parameter to the actual limits of the array seems not unreasonable as it prevents the dreaded "out of bounds" hard crash.

M23

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

Share this post

Share on other sites

My proposal is to change it to this:

```; Bounds checking
If \$iElement < 0 Then Return SetError(2, 0, False)
If \$iElement > \$iUBound Then SetError(3, 0, False)```

Which doesn't even attempt to catch every possible error.  Returning an error is less reasonable than an assumption about what the programmer meant?  The UDF also returns errors for other things too like passing non arrays where arrays are expected.

Edited by jaberwocky6669

Share this post

Share on other sites

^^That would be correct.

.

eMyvnE

Share this post

Share on other sites

jaberwocky6669,

I suggest you open a "Feature Request" in Trac with your suggested solution.

M23

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

Share this post

Share on other sites

Looks to me like 60% bug and 40% feature request, but anyway it's well worth posting in trac.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post

Share on other sites

Looks to me like 60% bug and 40% feature request, but anyway it's well worth posting in trac.

Correct. If the user is silly enough to pass values outside of the array's range then it's their own fault, but we shouldn't be deleting the 0th or UBound()th entry for their *insert-word-here*, though it's just deserts!

Share this post

Share on other sites

One last question before I do that.  Can someone confirm if this is an off by one error?

```; Bounds checking
If \$iElement < 0 Then Return SetError(2, 0, False)
If \$iElement > \$iUBound Then SetError(3, 0, False) ; if there are 10 elements then this only accounts for 11 and higher.

; Bounds checking
If \$iElement < 0 Then Return SetError(2, 0, False)
If \$iElement >= \$iUBound Then SetError(3, 0, False)```

Share this post

Share on other sites

Look at the code again. \$iUBound = UBound(...) - 1

Share this post

Share on other sites

I have added it for a release with v3.3.11.0. I am torn between adding it to the "script breaking changes" as those that use this function are less likely to error check their code. I will add it.

I don't use the Array UDF, so it makes no odds to me.

Edited by guinness

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

• Recently Browsing   0 members

No registered users viewing this page.

×
×
• Create New...