BB___DATA

=DATA= toc

Description
> The DATA statement holds a list of items that can be inserted into the program. The data is brought into the program with the READ command.

Syntax

 * 1) data //"One", "Two", "Three", "Four", "Five"//
 * 2) data //1, 2, 3, 4, 5//
 * 3) data //"Jim", 44, "Bob", 37, "Cliff", 53//

Hints
> DATA statements can be placed anywhere in the program but are typically placed at the end of the program listing where they can be easily edited or modified.

> DATA statements placed in subs or functions are "local" to the sub or function and cannot be READ by the main program. However, DATA statements within a GOSUB routine code block, which is accessed by a gosub(branchLabel) command, will be read in normal order. See Example.

> In Just BASIC, you cannot read data directly into an array. Data must first be READ into a variable before it can be assigned to an array element. See example.

> If your program tries to READ more DATA statements than are provided, you will cause an error. Consider placing a "flag" as the last element in your data statements, such as "end", and testing for this flag. Halt all READs when the flag is encountered.

> You can start READing the data over again with the first DATA statement (or with other, marked by label) by issuing the RESTORE command.

Example
code format="lb" ' Read and print column headings and row numbers. print "TABLE ONE" print for i = 1 to 5 read headg$ print headg$, next i   print

for i = 1 to 5 read row print row next i   print end

data "One", "Two", "Three", "Four", "Five" data 101, 22, 345, 447, 5 code

code format="lb" 'Using DATA statements to fill an array:

' In Just BASIC, data must first be read into a variable before ' it can be assigned to an array element. dim myArray$(5)           'dimension size of array for i = 1 to 5           'start a loop to read DATA read item$           'assign data item to variable myArray$(i) = item$   'assign variable to array element #(i) next i                   'get next DATA item '.   '.    data "Name", "Address", "City", "State", "Zip" end code

code format="lb" ' dimension array to 10 elements dim A(10)
 * DATA statements within a GOSUB routine**

' data statements... data 1,2,3,4,5,6,7,8,9,10 data 11,12,13,14,15,16,17,18,19,20

' This loop will read the contents for the first 3 DATA statements that it finds. ' It will first read data 1-10, then the next statement, data 11-20. ' The third data statement is found in the [morenumbers] branch, and data 31-40 ' will be read because data statements listed here are "seen" by the main program. for k = 1 to 3 for x = 1 to 10  ' This loop reads 10 numbers into the array READ y           A(x) = y        next x    next k

' program branches to the subroutine gosub [morenumbers]

for x = 1 to 10   ' This loop will PRINT the numbers in the array ... print A(x)    ' (that were loaded in the subroutine) next x   end

' This is the GOSUB subroutine. DATA statements in this subroutine are ' "global" and can be read by the main program. Be aware that data 31-40 ' has already been read earlier in the program.

[morenumbers] data 31,32,33,34,35,36,37,38,39,40   ' more data statements... data 41,42,43,44,45,46,47,48,49,50

for x = 1 to 10 READ y   ' Reads numbers 41 to 50 because numbers 31 to 40 are in                  ' the 3rd DATA statement which has already been read. A(x) = y   next x    return        'back to main program code

Useful Functions
code format="lb" Place a useful function using this command here code