Array initialization

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
wwwoholic
Posts: 53
Joined: 23 December 2010, 20:58 PM

Array initialization

Post by wwwoholic »

This question sounds really dumb...
is there syntax for array literals to initialize two-dimensional array constant?
I tried visual basic syntax and got a bunch of strange errors
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Can you provide more information about what you're trying to accomplish. ZBasic doesn't currently support initialization of array variables. However, you may be able to use a Program Memory Data Item for two dimension constant data.
- Don Kinzer
wwwoholic
Posts: 53
Joined: 23 December 2010, 20:58 PM

Post by wwwoholic »

Well, it is extremely simple. I need a constant look-up table of integer values, about 30x5. As a workaround I wrote InitRow subroutine with 6 parameters which is called 30 times from InitArray subroutine.

In standard Basic syntax it'll be something like this:
Dim numbers = {{1, 2}, {3, 4}, {5, 6}}

If you are asking how it is being used... the numbers in table are 5-dimensional coordinates of a set of 30 waypoints. There is a function that accepts coordinates and locates closest waypoint. Function returns an index of that row in a table.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

wwwoholic wrote:I need a constant look-up table of integer values, about 30x5.
Below is some example code using an Integer table. One unusual aspect of accessing tables in Program Memory is that the column index appears first followed by the row index. This is backward with respect to the way that variable arrays are indexed - it was done that way for backward compatibility with BasicX.

Code: Select all

Dim myTable as IntegerTableData ({
     1,  7, 235,   8, 21
    63, 77,  59,  82, 77
   106, 23,  11,  21,  9
})

Public Sub Main()
    Dim i As Integer, j as Integer
    
    For i = 1 to 3
        For j = 1 to 5
            Dim s as String
            s = "     " & CStr(myTable(j, i))
            Debug.Print Right(s, 5);
        Next j
        Debug.Print
    Next i
End Sub
- Don Kinzer
wwwoholic
Posts: 53
Joined: 23 December 2010, 20:58 PM

Post by wwwoholic »

Thanks a lot! It never occurred to me to look in "Program Memory Data Items" section of the manual. The order of indexes of course does not matter, as long we pay attention.

It is unfortunate though, that it is indexed starting with 1, which leads to unnecessary subtractions on every access.
Post Reply