neilsf / xc-basic3

A BASIC cross compiler for MOS 6502-based machines
MIT License
45 stars 5 forks source link

DATA syntax for two-dimensional array? #235

Closed sine707 closed 1 year ago

sine707 commented 1 year ago

Is it currently possible to populate a two-dimensional array with DATA? I wasn't able to find it in the docs and my experiments have failed thus far.

dim test_values(3, 3) as byte @testdata

testdata:
data as byte ???
neilsf commented 1 year ago

Two dimensional arrays are nothing more than linear data.

Try this:

DIM testvalues(3,3) AS BYTE @testdata

FOR i AS BYTE = 0 TO 2
  PRINT testvalues(0, i), testvalues(1, i), testvalues(2, i)
NEXT i

testdata:
DATA AS BYTE 1, 2, 3
DATA AS BYTE 4, 5, 6
DATA AS BYTE 7, 8, 9

' Note that the following is identical
' DATA AS BYTE 1, 2, 3, 4, 5, 6, 7, 8, 9
sine707 commented 1 year ago

Thank you! I think had the order wrong, as I was thinking testvalues(index, value) instead of the other way around. This makes sense now.

neilsf commented 1 year ago

I'm glad that I could help. It might not be intuitive how array elements are laid out in memory, but it is what it is.