Low() and High() functions like in Pascal/Delphi

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Low() and High() functions like in Pascal/Delphi

Post by kpeters58 »

Wish there were Low() and High() functions like in Pascal/Delphi (applicable to all data types that have defined low & high values).

Among other use cases, especially handy for looping over arrays in that it frees the programmer from having to use literal index numbers (similar to foreach).

One could do:

Code: Select all

Dim MyArray(666)
For index = Low(MyArray) to High(MyArray)
   MyArray(index) ...
Next
No more need to go through code to adjust loops etc. whenever the dim's of an array change
Last edited by kpeters58 on Fri Jun 07, 2019 4:11 pm, edited 1 time in total.
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Low() and High() functions like in Pascal/Delphi

Post by skywalk »

That will be slow for large arrays. You have 2 redundant calculations at top of For loop.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Low() and High() functions like in Pascal/Delphi

Post by Marc56us »

For Arrays, in PB low() are always 0 and for high() we have ArraySize()

:idea: it is also preferable to assign the function to a value to avoid recalculation at each iteration.

Code: Select all

Array_Size = ArraySize(MyArray())
For index = 0 To Array_Size
   MyArray(index) ...
Next
Last edited by Marc56us on Fri Jun 07, 2019 5:23 pm, edited 2 times in total.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Low() and High() functions like in Pascal/Delphi

Post by #NULL »

Marc56us wrote:For Arrays, in PB low() are always 0
There are some exceptions. JSON/XML extracted arrays can be empty. Global arrays declared in a procedure that is not called are also empty.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Low() and High() functions like in Pascal/Delphi

Post by kenmo »

#NULL wrote:
Marc56us wrote:For Arrays, in PB low() are always 0
There are some exceptions. JSON/XML extracted arrays can be empty. Global arrays declared in a procedure that is not called are also empty.
It still works: ArraySize() returns -1 for an empty extracted array, and a For loop from 0 to -1 simply doesn't execute.


I don't fully understand the original post. If you use ArraySize() or constants/variables for array sizes, you don't need to "adjust loops whenever the dims change".


A feature I would like to see is ForEach to work with Arrays, automatically handling the indexing. (Like a List or Map.)

Code: Select all

Dim Test.i(10)

ForEach Test()          ; Notice, no array index.
  Test() = Random(100)  ; Of course this syntax would only work within a ForEach loop
Next
Post Reply