Common in other Basic dialects, UBOUND() returns the number of the highest array element, i.e. the value specified in DIM and helpful for working with dynamic arrays.
MMBasic on the Micromite series of micro-controllers lacks this function. The below code searches the Variable Table for the named variable, returning the dimension as appropriate. Although not “beginner standard” following the code is quite easy and interesting as it devlves into the innards of one of the mechanisms of MMBasic. The function minimizes the number of checks by only examining variables that start with the same letter - this is a major update over the previous version and reduces the search time in the demo code by 90% - from ~170mS to just 17mS (testing at 5MHz to show granularity).
x=UBound("MyArray")
Returns the highest dimension of the array, or 0 if the array was not found.
'some demo code 'set up some variables Dim Integer aaaaaaaaaa,bbbbbbbbbb,ccccccccc,ddddddd,eee,ffff,gggg,hhhh,iiiii,j,k,llll 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 timer=0 k=UBound("A1") ?timer,k 'The function Function UBound(z$) As Integer' warning, destroys z$... shouldn't matter Local q$ Local Integer n,m,p z$=Ucase$(z$)+Chr$(0) p=Asc(Left$(z$,1)) For n=0 To 2047 Step 64 '2K of variable descriptors If p=Peek(VARTBL,n) Then 'potential match q$="" For m=0 To Len(z$)-1 'retrieve full name + NUL q$=q$+Chr$(Peek(VARTBL,n+m)) Next If Ucase$(q$)=z$ Then 'found the variable UBound=Peek(VARTBL,n+35)*256+Peek(VARTBL,n+34) Exit Function End If End If Next UBound=0 End Function