JohnOne

UBound Return Discussion

59 posts in this topic

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.

Share this post


Link to post
Share on other sites



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

I believe it is short for Upper Bound.

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

#4 ·  Posted (edited)

Not unreasonable, but I think it might be a little expensive (not sure how much). Sometimes you already know the size of the returned array and want to avoid any extra overhead.

Edited by czardas

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

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

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@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

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

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.


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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Look at my edit above. It is element zero that causes the issue. The definition of Ubound is not in any way to blame ambiguous or incorrect.

Edited by czardas

Share this post


Link to post
Share on other sites

It is the upper bound if you number the elements starting from 1. I suppose you could define it as the last index number instead. Does anyone do that?

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

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

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

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 (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

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.


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

Monkey's are, like, natures humans.

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