Jump to content

UBound Return Discussion


JohnOne
 Share

Recommended Posts

I find myself wrapping UBound a lot of late, when working with a lot of arrays.

$str = "five words in this string"

$a = StringSplit($str, " ", 2)

MsgBox(0, "Upper Bound", _UBound($a))

Func _UBound(ByRef $array)
    Return UBound($array) - 1
EndFunc   ;==>_UBound

Why is that function called "UBound" anyway, it's more "Size" or "Length"

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

Is it reasonable to lobby for @extended for stringsplit to be set to ubound($return) - 1  ?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

true, in most cases directly after stringsplit i have  $max = ubound($a) - 1 , that's been so ingrained I have no issue if there is even the slightest of penalty.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@JohnOne I get where you are coming from, however I tend to use both 0-based and 1-based arrays: depending on circumstances. I would consider a 0-based array to be the general case and Ubound defining the number of elements is easy to work with. When I declare / redim etc... I want to use the same definition for array size (measured by the number of elements) to maintain consistency.

Edit
I don't like the following syntax so much:

Redim $aColExpand [Ubound($aColExpand) +1][$iCols +1] ; maintain the same number of rows

 

Edited by czardas
Link to comment
Share on other sites

 

@JohnOne I get where you are coming from, however I tend to use both 0-based and 1-based arrays: depending on circumstances. I would consider a 0-based array to be the general case and Ubound defining the number of elements is easy to work with. When I declare / redim etc... I want to use the same definition for array size (measured by the number of elements) to maintain consistency.

It does not matter what index you use as your first, upper bound remains the same.

A full array iteration For To Loop wants the last element to be processed, to me that is the upper bound, not the one after it.

It's why I wrapped it, every loop has to have For $n = $x To Ubound($array) -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

I know it's not ambiguous, it gives you what the help file says it does...

the size of the array dimension or the number of keys within a map

The size.

I'm saying it's wrong to call it UBound.

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

But you don't get to number the elements, AutoIt does that.

Local $Array[10] = [1,2,3,4,5,6,7,8,9,10]

For $i = 1 To UBound($Array)
    ConsoleWrite($i & " " & $Array[$i] & @LF)
Next

 

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

Okay I agree that the name UBound is an unfortunate choice, in so much as the convention of including an element numbered zero is inconsistent with the strict interpretation of the word bound. Think of a ruler with 12 inches: here the upper bound of the ruler is 12 and the lower bound is zero. An array with 12 elements would strictly speaking be analogous to to a ruler with 11 inches: confusing!

Edited by czardas
Link to comment
Share on other sites

I think element 0 as a special place... similar to period 0 in schools where you get to do the pledge :P.

Element 0 has no special meaning in AutoIt. I once had a discussion with a Dev when rewriting the Excel or Word UDF (don't know which one).
It was clearly stated, that element 0 should not hold the element count. That's what UBound is made for.
How would you know if a function returns a 0- or 1-based array? Using 1-based arrays is bad coding practice I was told. Unfortunately some of my downloadable UDFs already used 1-based arrays.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water :o, I don't think 1 based arrays are not *that* bad... btw I know that element 0 has no special meaning but thinking it as special helps me a lot while coding :D!

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Okay I agree that the name UBound is an unfortunate choice, in so much as the convention of including an element numbered zero is inconsistent with the strict interpretation of the word bound. Think of a ruler with 12 inches: here the upper bound of the ruler is 12 and the lower bound is zero. An array with 12 elements would strictly speaking be analogous to to a ruler with 11 inches: confusing!

lol, no no no, I'm not falling for that, a ruler has no 0th element

Maybe I just don't understand. perhaps someone could show me a couple of uses for Ubound on its own without deducting from it, maybe one which is not just for getting it's length, which is what a size should be for.

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

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