DATA data1 [, data2 [, data3 ... ] ] DATA [AS data type] data1 [, data2 [, data3 ... ] ] DATA LOAD "filename" AS TEXT
The DATA
command is used to create a list of data that will be used
in other parts of the program. It is like a container where values are
stored that will then be “read” and used by other commands.
The DATA
command is usually placed at the beginning of the program or in
a section dedicated to the definitions of data, but it is not mandatory.
The data1
, data2
, … so on should be replaced witht he data you want to
store inside the program. These can be numbers or text strings. To read the
data contained in a DATA
statement, you use the READ
command.
This command assigns the values contained in DATA
to variables.
You can use multiple DATA
statements to fill an array with a set of values.
Another usage is to create small tables of data, for example to represent
menus or product lists. You can also store constants that are used frequently
in the program.
The DATA
values will be read from left to right, beginning with the first
line containing a DATA
statement. Each time a READ
instruction is
executed the saved DATA
position of the last READ
is advanced to the
next value. Strings must be written in quotes, so characters
like comma, space, colon, graphical ones or control characters has to be
written inside double quotes like string constants. The RESTORE
resets
the pointer of the current DATA
position the program start so that
next READ
will read from the first DATA
found from the beginning
of the program.
The instruction DATA
stores numeric data in an optimized way: so, if you enter a numeric
constant that can be represented by a single byte, it will be stored in the program
as a single byte. Floating point numbers are stored with default precision.
Finally, strings are stored “as is”. As a result, when you use the READ
command,
ugBASIC will implicitly perform the conversion if the same data type is not used,
and if it is posssible. It is possible to avoid this behavious by using the AS
keyword, followed by the data type.
Separates the definition of data from its use, makes the code more readable and maintainable. You can change the data without having to change the logic of the program. Finally, the same data can be read multiple times in different parts of the program.
There is also a version of the syntax that allows you to load values from an external file. This syntax is currently limited to numeric data that must be separated by a non-numeric separator.
DATA 10, 20, "test" DATA AS INTEGER 10, 20, 30 DATA LOAD "datas.txt" AS TEXT
See also the following example files:
Da
If you have encountered a problem using this command, if you think there is a bug or the explanation is unclear, please open an issue for this keyword on GitHub. Thank you!