JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 00:41 06 Dec 2022 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic UBound() function

Author Message


Joined: 07/09/2016
Location: United Kingdom
Posts: 1855
Posted: 05:32pm 18 Nov 2022
Copy link to clipboard 
Print this post

evening all... please find below a working UBound() function. Tested on a MicroMite Mk2 so beware on later platforms as variables are handled subtly differently so may not work.

I do stuff with dynamic arrays and the lack of a UBound function has been a pain for me for a while. Not any more. Enjoy

The Function is documented here but if you just want to get to the goodies, here it is below.

If anyone can tell me how to improve - especially detecting the end of the variable table properly, please do.

Beware, variable names longer than 31 chars will cause errors due to me not caring enough to check (and to speed things up)

Dim Integer a,b,c,d,e,f,g,h,i,j,k,l
Dim  a1$(17)

'same size as we put above
Print UBound("A1")

'dump the array
Erase a1$

'and re-create with unknown dimensions
Dim a1$(Int(Rnd*20)+1)
'not unknown anymore
Print UBound("A1")

Function UBound(z$) As Integer' warning, destroys z$... shouldn't matter
Local q$
Local Integer n,m
For n=0 To 2047 Step 64 ' 2K of variable descriptors... how do I tell the end of the table?
For m=0 To Len(z$)-1 'retrieve the variable name + Chr(0)
q$=q$+Chr$(Peek(VARTBL,n+m) )
If q$=z$ Then ' found the variable
Exit Function
End If
End Function

Edited 2022-11-19 03:40 by CaptainBoing


Joined: 29/01/2013
Location: Canada
Posts: 938
Posted: 09:49pm 18 Nov 2022
Copy link to clipboard 
Print this post

Ah, looks like only the CMM2 and the Armite F4 have the BOUND(array() [,dimension]) function built in.

If killing z$ were an issue, you could just assign it to a string variable local to the function.

Also, if you only DIM, say, Zaphod$(42), will UBound("z") return 42?
Visit Vegipete's *Mite Library for cool programs.


Joined: 07/09/2016
Location: United Kingdom
Posts: 1855
Posted: 06:47am 19 Nov 2022
Copy link to clipboard 
Print this post

morning Pete

Yes it was an addition to later versions. I program, almost exclusively, for the diminutive MicroMites and occasionally the MMX and H7/F4. None of these has Ubound (at least the version of MMBasic I have loaded).

The Z$ trashing is better avoided by enclosing the argument in parenthesis, but I really can't see that it's a biggie, I wanted to avoid more variables on small memory systems. It's only a problem if you use a variable to hold the name of the variable... (wow, I nearly went back in time there for a moment   ).

In answer to your question; no. MMBasic only allows a single instance of a name, so you can't have Z$ and Z% which means I don't have to check the variable type in the VARTable. I search for the complete name, so in the instance you give:


will give 42 (I see what you did there   )

and, (assuming you have no variable called z which is dimensioned similarly)


will return 0, meaning no dimensioned variable called "z" was found (can't have a zero dimensioned array even with option base 0)

p.s. Sorry it is late Jean, RIP
Edited 2022-11-19 18:18 by CaptainBoing
Print this page

To reply to this topic, you need to log in.

© JAQ Software 2022