AW: [bascom] Two-dimensional Arrays?


From Vögel Franz Josef <FranzJosef.Voegel@telekom.at>
Date Tue, 21 Oct 2003 11:51:20 +0200
Thread-index AcOXoe0BrPoPaNJjRqmAkIi1zUg66gAFdZyw
Thread-topic [bascom] Two-dimensional Arrays?

Hi Marc,

You can do it by yourself with 2 user defined functions:

Here you have an example of 2-dim array of type byte, but in can easily 
adapted
to other types (and higher dimensions too).

The sub SetArray2D write a value to the 2d-array
The function GetArray2D read a value from the 2d-Array
Note: the index in the dimension is one-based, that is starting from 1 to 
max. value

'--------------------------------------------------------------------------------

Const cArrayD1 = 7                        ' define here Size of dimension 1
Const cArrayD2 = 13                       ' define here Size of dimension 2

Const cArrayD1D2 = cArrayD1 * cArrayD2    ' sum of elements in 2d-Array

Dim bArray2D(cArrayD1D2) as Byte        ' hold the data of the 2d-array

Declare Function GetArray2D (D1 as Byte, D2 as Byte) as Byte
Declare Sub SetArray2D (D1 as Byte, D2 as Byte, pValue as Byte) as Byte


Dim b1 as Byte          ' counter for D1
Dim b2 as Byte          ' Counter for D2
Dim b3 as Byte          ' array value to write and read
b3 = 0
print "Writing a value to each element of the 2d-array"
print "D1 D2 Value"
for b1 = 1 to cArrayD1
   for b2 = 1 to cArrayD2
      setArray2D b1, b2, b3
      print b1; " " ; b2 ; " " ; b3
      incr b3
   next
next


print "Reading each element of the 2d-array"
print "D1 D2 Value"
for b1 = 1 to cArrayD1
   for b2 = 1 to cArrayD2
      b3 = GetArray2D(b1, b2)
      print b1; " " ; b2 ; " " ; b3
   next
next

end



Function GetArray2D (D1 as Byte, D2 as Byte) as Byte
   Local WArrayPointer as Word
   wArrayPointer = d1 - 1
   wArrayPointer = wArrayPointer * cArrayD2
   wArrayPointer = wArrayPointer + D2
   GetArray2D = bArray2D(wArrayPointer)
end Function



Sub SetArray2D (D1 as Byte, D2 as Byte, pValue as Byte) as Byte
  Local WArrayPointer as Word
  wArrayPointer = d1 - 1
  wArrayPointer = wArrayPointer * cArrayD2
  wArrayPointer = wArrayPointer + D2
  bArray2D(wArrayPointer) =  pValue
end Sub


'-----------------------------------------------------------------
Best regards Josef

-----Ursprüngliche Nachricht-----
Von: Marc Reviel [mailto:marc@powerlogix.com] 
Gesendet: Dienstag, 21. Oktober 2003 09:02
An: bascom@grote.net
Betreff: [bascom] Two-dimensional Arrays?


Any plans for BASCOM to implement 2d Arrays?  Pretty useful addition.

-Marc