Available on:

DATA

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.

SYNTAX

 DATA data1 [, data2 [, data3 ... ] ]
DATA [AS data type] data1 [, data2 [, data3 ... ] ]
DATA LOAD "filename" AS TEXT


Legend
  • id : identifier
  • type : datatype
  • v : value
  • "..." : string
  • [...] : optional

EXAMPLE

 DATA 10, 20, "test"
 DATA AS INTEGER 10, 20, 30
 DATA LOAD "datas.txt" AS TEXT


Used in:

ABBREVIATION: Da

Join BASIC 10Liner Contest with ugBASIC!

An interesting competition is held at the beginning of each year: the BASIC 10Liner Contest. It is possible to use ugBASIC to participate in the next "BASIC10Liner" competition, in the following categories:

  • PUR-120 - A game in 10 lines of max 120 characters (w/abbrev.)
  • EXTREME-256 - A game in 10 lines of max 256 characters (w/abbrev.)
  • SCHAU - Any program in 10 lines of max 256 characters (w/abbrev.)
In order to reduce space you can use this abbreviation for this instruction:

DATA ↔ Da

Any problem?

If you have found a problem with this keyword, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO KEYWORDS