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 'Size of the aray
DIM ARRAY[A] 'Declaring the array and its size
RESTORE @TESTDATA 'Points where to start reading the data
FOR I=0 TO A-1 'From 0 to 9 = 10 iterations for each data
READ C 'With READ you need to store all the data in a temporary variable, in this case C
ARRAY[I]=C 'Now store each data declaration to the array
NEXT
FOR I=0 TO A-1 'This FOR loop is for printing the data
PRINT STR$(ARRAY[I]) 'This converts the data to string
NEXT
END
@TESTDATA 'DATA label
DATA 4,9,2,16,86,12,5,129,56,0 'You can use a comma to separate DATA or use a new DATA line for each one.
Example #2 - Two-Dimensional Array
CLS
A=3 'Number of rows
B=2 'Number of columns
DIM ARRAY[A,B] 'The first one defines the # of rows, the second one the # of columns
RESTORE @TESTDATA
FOR I=0 TO A-1
FOR J=0 TO B-1
READ C
ARRAY[I,J]=C 'The first read is in position 0,0 then 0,1 inside the array. After that it goes into the first loop again and continues with the next row.
NEXT
NEXT
FOR I=0 TO A-1
FOR J=0 TO B-1
PRINT STR$(ARRAY[I,J]);" "; 'I put a blank space to separate each column
NEXT
PRINT 'I put this empty PRINT to create a new line for each row.
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.
10 Comment(s)randoIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthThird YearMy account is over 3 years oldWebsiteAvatar TabooI didn't change my avatar for 180 daysWebsiteWonder if I could make a 3d game with a 3 dimensional array...
jkjk I know I need an engine for 3drandoIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthThird YearMy account is over 3 years oldWebsiteAvatar TabooI didn't change my avatar for 180 daysWebsiteNow that I think about it, I can store data with one for the map.MidnocloseZelda Is Awesome!I love The Legend Of Zelda!Express YourselfNight PersonI like the quiet night and sleep late.Express YourselfSecond YearMy account is over 2 years oldWebsiteI keep getting an Out of DATA error.PtcguyFirst WeekJoined in the very first week of SmileBASIC SourceWebsiteMinecraft Is Awesome!I love Minecraft!Express YourselfBeginner ProgrammerI'm just starting out! I'm still trying to grasp the basics.Programming StrengthIs there a way to enlarge arrays?snail_Power UserQSP Contest 1 Contest ParticipantI participated in the first SmileBASIC Source QSP Contest!HelperReceived for being very helpful around SmileBASIC SourceAchievementsAmazing ContributorSomeone thinks I'm an awesome person who has done so much for the community!Achievements2D arrays? No. 1D arrays, you can PUSH empty entries to the top or SHIFT empty entries to the bottom.12Me21Syntax HighlighterReceived for creating the code syntax highlighter on SBSNight PersonI like the quiet night and sleep late.Express Yourselfyou can enlarge arrays with = or COPYPixelStudioOh also what about words like"hello"PixelStudioWould two dimensional arrays work as a horizontal side scrolled on a 2d game
SquareFingersREAD C 'With READ you need to store all the data in a temporary variable, in this case C
Not true. READ ARRAY will work just fine.
PRINT STR$(ARRAY) 'This converts the data to string
PRINT does not require the data to be converted to string, using STR$ here is a needless complication.Super_DefaultioVideo GamesI like to play video games!HobbiesDay PersonI like the warm sunshine and wake up early!Express YourselfZelda Is Awesome!I love The Legend Of Zelda!Express YourselfI like this tutorial, but what would make it better if you added the syntax for the commands.