Jump to content

Recommended Posts

Posted

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.

Posted

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

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

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
Posted

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.

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

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
Posted

 

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

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
Posted

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 2024-07-28 - Version 1.6.3.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 (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

 

Posted

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.

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