Τετάρτη 21 Ιανουαρίου 2026

Array Length

https://rosettacode.org/wiki/Array_length#M2000_Interprete

Update using the three different objects for array: tuple, mArray, RefArray. Old versions of M2000 had only mArray, always as Variant (and tuple was mArray), then a RefArray used with squared brackets but using various item size from Byte, then mArray also drive a RefArray (using dimensions over RefArray - which the second object is hidden) so we may have A(10,2) as Byte, then tuple are an object always flat and for variants only. The B()=A when A point to tuple copies to B() as mArray. An mArray is heavier object, because holds array for one to ten dimensions of values for low and high bound, p;us an array for each dimension for offsets on a flat array (1d)  for item values (or pointer to RefArray, if we use specific type). A pointer to array may point to tuple or mArray. A pointer to RefArray can't change to anything else (can be value for a variant type variable or array item). A RefArray may have one or two dimensions, but we can construct any number by using a variant type RefArray using each item a refArray and so on. So we can make jagged arrays, using RefArrays. We can do the same using mArray of type variant or tuple. So A=((,), (1,), (1,2), (1,2,3)) is a jagged array of type tuple. The (,) is an empty tuple (length zero). For mArray arrays the A()=B() is a copy of B() to A(). and A()=A when A is pointer to mArray or tuple is also a copy, but A=B() is a pointer to B() copied to A.

' A is a pointer to array
A=("Apple", "Orange")
Print  Len(A)=2 ' True
Print Type$(A)="tuple"
Dim A(10,2) as Byte=32, B(), C(10,2)
Print Len(A())=20, Len(B())=0, Len(C())=20
C(1,1)="String"
C(2,0)=100, 3
Dim C(6,2)
Print Len(C())=12
Print C(2,0)=100, C(2,1)=3, C(1,1)="String"
B()=A ' this is a copy
A=(,)
Print B(1)="Orange", Len(B())=2, Len(A)=0
Print Type$(B())="mArray", Type$(A)="tuple"
A=B() ' now A point to B()
Print Type$(A)="mArray"
Print A(5,1)=32, Type$(A(5,1))="Byte"
A=A() ' point to A() we address through A as a flat 1d array.
Print Type$(A,0)="Byte", A#val(0)=32, A#sum()=32*20, A()#sum()=32*20
Print Dimension(A())=2 ' Number of dimensions
Print Dimension(A(), 1)=10, Dimension(A(), 2)=2 ' length for each dimension
Print Dimension(A(), 1, 0)=0, Dimension(A(), 1, 1)=9 ' lower and upper bound on 1st Dimension


' RefArray type of Arrays (always 0 lower bound)
Variant D[100]
Print Len(D)=101, Type$(D)="RefArray"
Byte F[9][1]
Print Len(F)=10
F[3][0]=111
F[3][4]=105
D[3]=F[3][] ' copy a row from F[3] to D[3]
F[3][0]++

Print D[3][0]=111, D[3][4]=105, F[3][0]=112