JohnOne Posted December 12, 2015 Share Posted December 12, 2015 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 ;==>_UBoundWhy 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 More sharing options...
czardas Posted December 12, 2015 Share Posted December 12, 2015 Why is that function called "UBound" anyway, it's more "Size" or "Length"I believe it is short for Upper Bound. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
iamtheky Posted December 12, 2015 Share Posted December 12, 2015 Is it reasonable to lobby for @extended for stringsplit to be set to ubound($return) - 1 ? ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
czardas Posted December 12, 2015 Share Posted December 12, 2015 (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 December 12, 2015 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
iamtheky Posted December 12, 2015 Share Posted December 12, 2015 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 More sharing options...
JohnOne Posted December 12, 2015 Author Share Posted December 12, 2015 @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. 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 More sharing options...
JohnOne Posted December 12, 2015 Author Share Posted December 12, 2015 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. 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 More sharing options...
TheDcoder Posted December 12, 2015 Share Posted December 12, 2015 In the mean while, I simplified my code to 1 line 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 More sharing options...
czardas Posted December 12, 2015 Share Posted December 12, 2015 (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.EditI don't like the following syntax so much:Redim $aColExpand [Ubound($aColExpand) +1][$iCols +1] ; maintain the same number of rows Edited December 12, 2015 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
TheDcoder Posted December 12, 2015 Share Posted December 12, 2015 I think element 0 as a special place... similar to period 0 in schools where you get to do the pledge . 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 More sharing options...
JohnOne Posted December 12, 2015 Author Share Posted December 12, 2015 @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 More sharing options...
czardas Posted December 12, 2015 Share Posted December 12, 2015 (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 December 12, 2015 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
JohnOne Posted December 12, 2015 Author Share Posted December 12, 2015 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 mapThe 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 More sharing options...
czardas Posted December 12, 2015 Share Posted December 12, 2015 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? operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
JohnOne Posted December 12, 2015 Author Share Posted December 12, 2015 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 More sharing options...
czardas Posted December 12, 2015 Share Posted December 12, 2015 (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 December 12, 2015 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
TheDcoder Posted December 12, 2015 Share Posted December 12, 2015 I got an idea for a new FAQ my Arrays 101 topic, Thanks JohnOne! 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 More sharing options...
water Posted December 12, 2015 Share Posted December 12, 2015 I think element 0 as a special place... similar to period 0 in schools where you get to do the pledge .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 Link to comment Share on other sites More sharing options...
TheDcoder Posted December 12, 2015 Share Posted December 12, 2015 @water , 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 ! 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 More sharing options...
JohnOne Posted December 12, 2015 Author Share Posted December 12, 2015 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 elementMaybe 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now