Jump to content

UBound Return Discussion


JohnOne
 Share

Recommended Posts

@JohnOne I think I'm not wording this very well. There is no inch (number) zero on a ruler, but there is an element zero in an array. The terms 'upper and lower bounds' make perfect sense on a ruler.

Edit
Bounds are not elements (nor inches): they are (by definition) boundaries.

Edited by czardas
Link to comment
Share on other sites

  • Moderators

If I may,

I can see 2 uses for such a function:

  • Getting the number of elements in an array so that other variables might be sized accordingly.
  • Getting the top index of the array - which is what the VBasic version of the function does.

As AutoIt uses 0-based arrays these 2 values are not the same, with the first always being one more than the second. So there was a choice to be made (by Valik I believe) when the function was added to AutoIt:

; What we do now

Local $aNewArray[UBound($aOldArray)]

For $i = 0 To UBound($aOldArray) - 1

or

; What might have happened

Local $aNewArray[UBound($aOldArray) + 1]

For $i = 0 To UBound($aOldArray)

I see pros and cons in both approaches - but I am now so used to the way we have to do it. Personally I feel the function is wrongly named, but as the return value is quite specifically described in the Help file I see no problem with what it actually means.

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 think the function is correctly named:

Local $aRuler[12] ; Upper bound = 12
For $i = 0 To Ubound($aRuler) -1 ; final entry
    $aRuler[$i] = "inch " & ($i +1) ; the data represents a length value (in one dimensional space) with a start point and an end point
Next

 

Edited by czardas
Link to comment
Share on other sites

I think the function is correctly named:

Local $aRuler[12] ; Upper bound = 12
For $i = 0 To Ubound($aRuler) -1 ; final entry
    $aRuler[$i] = "inch " & ($i +1) ; the data represents a length value (in one dimensional space) with a start point and an end point
Next

 

By your logic, -1 should be the lower bound of an array.

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Well bounds normally apply to limits, and not the information between them, so I disagree. A ruler is a perfect analogy. There are 13 callibrations on a 12 inch ruler (numbered 0 to 12). The elements of the array are like the inches of a ruler.

Edited by czardas
Link to comment
Share on other sites

No! 0 is the element enumeration convention being used here. The lower bound is still 0. The element value (also) encompasses all values elements between 0 and 1 (for example half an inch on my ruler). There is no such thing as half an array element though, but other than that the description fits perfectly as far as upper and lower bounds are concerned.

Edited by czardas
Link to comment
Share on other sites

So if we're talking in box logic, 9 is the logical upper bound of a 10 element array if 0 is the lower bound. After 9 some imaginary boundary which counts for nothing is end of it, not 10.

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

lower bound = 0           internal boundary = 1             upper bound = 2
     | ....... element 0 ........  | ........ element 1 ........  |
    [0]                           [1]                            [2]          <<< boundaries

 

Edited by czardas
Link to comment
Share on other sites

So what we are getting the information for when looping through an array, if the units we use is the boundary and and not the element?

That shows that we get the element immediately to the right of the boundary, so the final usable boundary in that representation is 1.

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

That shows that we get the element immediately to the right of the boundary, so the final usable boundary in that representation is 1.

Yep exactly that! :thumbsup:

Ubound means upper boundary - and not the final index number. This is not a misnomer.

Edited by czardas
Link to comment
Share on other sites

I see, as I suspected. We'll have to agree to differ though, because I think that in the context of scripting, that is plain stupid, since there is no real separator between memory addresses, I believe the boundaries ought to be the first and last indices on an array.

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Most containers have more than one side, otherwise (in the real world) everything would simply just spill out. The term comes the upper limits of a range - everything within the range is less than this limit (both by definition and by convention) since the limit itself is not part of the container (it is the edge of the container). Change that and the the terminology 'upper bound' would no longer be applicable.

Having said this, I understand that in other situations the upper bound may be reachable, and that the term is perhaps used rather loosely in some cases.

Edited by czardas
Link to comment
Share on other sites

Lets wrongly agree for a second that an autoit variant is 4 bytes long.

The first address of an 4 element "array" of those variants is 200, the next is 204, the next 208 and the last is 212.

There is no 216 to mark the end of it, and in fact a container might be spread across non sequential addresses and if they had boundaries would be more than 5.

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm more concerned with the definition of the word than how it is implemented. It really means a limit which you cannot go beyond, but in terms of an array (in AutoIt), it cannot be associated with any element existing within the array.

Edited by czardas
Link to comment
Share on other sites

czardas, you are rationalizing. Again :P.

Anyway, JohnOne is right, 100%. AutoIt is probably the only language in the world that reports Ubound() like that. VBS uses JohnOne's logic, C# also.
So, what now?

Thanks trancexx. I looked at VB and it gave me this: https://msdn.microsoft.com/en-us/library/fhx59d0t(v=vs.84).aspx

Look at the code and compare it to the description: there's something wrong on that page (I thought it was the description). Anyway I asked earlier if anyone did this - so now you say they do.

Dim A(100,5,4)
UBound(A, 1)
' returns 100

...

Returns the largest available subscript for the indicated dimension of an array.

Contradiction?

Edited by czardas
Link to comment
Share on other sites

Thanks trancexx. I looked at VB and it gave me this: https://msdn.microsoft.com/en-us/library/fhx59d0t(v=vs.84).aspx

Look at the code and compare it to the description: there's something wrong on that page (I thought it was the description). Anyway I asked earlier if anyone did this - so now you say they do.

Nope, everything is right there. Free your mind out of AutoIt and reread again.
VB uses this syntax to declare array:

Dim Arr(100)

...but that's shorter for:

Dim Arr(0 To 100)

Which means "Create array which first index is 0 and last is 100". How many elements are there?

Edited by trancexx

♡♡♡

.

eMyvnE

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