Jump to content
JohnOne

UBound Return Discussion

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"

Share this post


Link to post
Share on other sites

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


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

Share this post


Link to post
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.


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

Share this post


Link to post
Share on other sites

@extended is most likely set to 0 anyway, if that's true then setting it to the upper bound or length of the internal variant would not cost anything I believe.

Share this post


Link to post
Share on other sites

I believe it is short for Upper Bound.

Yes, but It's not the upper bound is it, It's the size of the array, which is 1 more than the upper bound, and why -1 is needed.

Share this post


Link to post
Share on other sites

In the mean while, I simplified my code to 1 line :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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

Share this post


Link to post
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.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

:idea: I got an idea for a new FAQ my Arrays 101 topic, Thanks JohnOne! :D


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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!


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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.

Share this post


Link to post
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

×
×
  • Create New...