From Swordfish Wiki

SwordfishUser: Multi Dimensional Arrays in Swordfish

Swordfish doesn't support arrays of more than one dimension directly, but it's very easy to achieve the same thing using structures. The syntax is perhaps a little more convoluted, but the net result is the same. You will also find it to be more flexible, as structures can hold different data types. However, lets keep it simple and look at how we can create a 10 x 10 matrix of byte values.

Structure TArray
   a(10) As Byte
End Structure
Dim Items(10) As TArray

The above code may look a little strange, but on closer examination it can be seen that Items() holds 10 structure elements, each with their own 10 byte array. To access an element, we simply use

Items(5).a(5) = 10

which is the same as writing something like

Items(5)(5) = 10

Here is a small code example which initialises a 10 x 10 matrix, then displays via the MCUs USART

// import modules...
include "usart.bas"
include "convert.bas"

// declare a 10 x 10 byte array...
Structure TArray
   a(10) As Byte
End Structure
Dim Items(10) As TArray

// local variables...
Dim i,j, Value As Byte

// initialise the two dimensional array...
for i = 0 to 9
   for j = 0 to 9
      Items(i).a(j) = i + j
   next
next

// display to the screen...
setbaudrate(br115200)
for i = 0 to 9
   for j = 0 to 9
      Value = Items(i).a(j)
      Write(DecToStr(Value,2)," ")
   next
   Write(13,10)
next

That's all there is to it. Passing your array to a sub or function is really easy also. For example, given the subroutine declaration

sub MySub(byref pArray() as TArray)
   ...
end sub

we can pass the array like this

MySub(Items)
Retrieved from http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.MultiArray
Page last modified on June 17, 2008, at 06:30 PM