Description
DIM is a function that declares an array and its dimensions. You can use up to 4 dimensions.
READ is a function that reads the data from
DATA. By default, it starts reading from the beginning of the first
DATA declaration.
DATA is a function that stores any kind of data (numbers and strings/characters) for it to be read. You can organize the data for better reading if you're using 2 dimensions or more (See Example #2).
RESTORE is a useful function for data reading. Instead of allowing
READ to read data starting from the beginning, you can make it start directly from the label.
You can use a
FOR loop to read all the data and store it in an array for future implementation.
Example #1 - One-Dimensional Array
CLS
A=10
DIM ARRAY[A]
RESTORE @TESTDATA
FOR I=0 TO A-1
READ C
ARRAY[I]=C
NEXT
FOR I=0 TO A-1
PRINT STR$(ARRAY[I])
NEXT
END
@TESTDATA
DATA 4,9,2,16,86,12,5,129,56,0
Example #2 - Two-Dimensional Array
CLS
A=3
B=2
DIM ARRAY[A,B]
RESTORE @TESTDATA
FOR I=0 TO A-1
FOR J=0 TO B-1
READ C
ARRAY[I,J]=C
NEXT
NEXT
FOR I=0 TO A-1
FOR J=0 TO B-1
PRINT STR$(ARRAY[I,J]);" ";
NEXT
PRINT
NEXT
END
@TESTDATA
DATA 1, 2
DATA 3, 4
DATA 5, 6
Two-dimensional arrays are really useful when programming 2D games, but it has other uses too.
Source
I used the information from this GameFAQs PetitComputer answer
http://www.gamefaqs.com/ds/663843-petit-computer/answers/331284-using-data-read-and-restore and updated it for SmileBASIC and added my own commentary.