Data

Data
You can store data items within the program's code using **Data** statements. All data statements are part of the program's data and are read in turn, from the beginning of the program code to the end. Data statements can contain both string and numeric items. Data is read with the **Read** statement, which reads each data item in turn and stores it in the designated variable or in the designated array index. The variable type must match the data type. The **Restore** statement sets the data pointer back to the beginning of the program's data.

The syntax for the statements is as follows:

DATA item1[,item2...] //- Define data items for the program// READ var1[,var2...] //- Get information from DATA statements and place into 1 or more vars// RESTORE //- Reset the DATA pointer back to the beginning//

Be sure that you do not attempt to **Read** more items than are contained in the program's **Data** statements, or you will get an error. Either count the items carefully, or use some sort of ending flag and check for that flag each time you **Read** data.

The following demo shows how to create data, how to read it into a variable and print the contents of the variable. It also shows how to reset the data pointer to the beginning, and how to read data into arrays.

code format="vbnet" 'begin the program's data data "apple","orange","banana","pear","peach","plum"

for i = 1 to 9 read fruit$ print fruit$ next

'reset to beginning of data restore

print print "The first fruit in the data block is " read fruit$ print fruit$ print

'reset to beginning of data restore

'now fill an array from the program's data 'data can be read directly into an array dim fruits$(9) for i = 1 to 9 read fruits$(i) print "fruits$(";i;") is ";fruits$(i) next

'more data data "cherry","strawberry","mango" code

The following demo shows how to use both string and numeric items of data. It also shows how to read more than one item of data with a **Read** statement.

code format="vbnet" data "pumpkins",37,"potatoes",52,"onions",16

for i = 1 to 3 read veg$,num print "Vegetable is ";veg$;" and number is ";num next code

This demo reads data in a while/wend loop, checking for an end of data flag.

code format="vbnet" data "pumpkins",37,"potatoes",52,"onions",16,"none",0

'when the item "none" is read, the program stops reading data while veg$ <> "none" read veg$,num print "Vegetable is ";veg$;" and number is ";num wend print "All data has been read." code