Liberty BASIC Help Online

READ and DATA
DATA statements can be embedded in Liberty BASIC programs to make access to string and numeric data easier.  DATA is accessed by the program with the READ statement, which reads the DATA items in sequence.  The RESTORE statement causes the next READ statement to start at the first DATA statement listed, or at the first DATA statement following a specified branch label if one is included in the RESTORE statement.  These methods can be used to put data into arrays, thus providing an easy method for filling arrays.
 
DATA
The DATA statement provides a convenient way to insert data into programs. The DATA can be read once or many times using the READ statement.  A DATA statement doesn't actually perform an action when it is encountered in the program's code.
 
  data "one", 1, "two", 2, "three", 3, "end", 0
 
One or more DATA statements form the whole set of data elements.  For example, the data represented in the example above can also be listed in more than one DATA statement:
 
  'here is our data in two lines instead of one
  data "one", 1, "two", 2
  data "three", 3, "end", 0
 
DATA is local to the subroutine or function in which it is defined.
 
READ
This fetches the next strings and/or numeric values from DATA statements in a program.  The READ statement will fetch enough items to fill the variable names that the programmer specifies.  The values fetched will be converted to fit the variables listed (string or numeric).
 
Example:
 
  'read the numbers and their descriptions
  while desc$ <> "end"
    read desc$, value
    print desc$; " is the name for "; value
  wend
  'here is our data
  data "one hundred", 100, "two", 2, "three", 3, "end", 0
  end
 
You can also read numeric items:
 
  'read the numbers and their descriptions
  while desc$ <> "end"
    read desc$, value$
    print desc$; " is the name for "; value$; ", length="; len(value$)
  wend
  'here is our data
  data "one hundred", 100, "two", 2, "three", 3, "end", 0
  end
 
RESTORE
 
RESTORE will reset the reading of DATA statements so that the next READ will get information from the first DATA statement in the program (or the first DATA statement in a function or subprogram, if this is where the RESTORE is executed).
 
Example:
 
  'show me my data in all uppercase
  while string$ <> "end"
    read string$
    print upper$(string$)
  wend
  string$ = ""  'clear this for next while/wend loop
 
  'now reset the data reading to the beginning
  restore
 
  'show me my data in all lowercase
  while string$ <> "end"
    read string$
    print lower$(string$)
  wend
 
  data "The", "Quick", "Brown", "Fox", "Jumped"
  data "Over", "The", "Lazy", "Dog", "end"
 
  end
 
RESTORE [branchLabel]
 
Optionally, you can choose to include a branch label:
 
  'show me my data in all uppercase
  while string$ <> "end"
    read string$
    print upper$(string$)
  wend
  string$ = ""  'clear this for next while/wend loop
 
  'now reset the data reading to the second part
  restore [partTwo]
 
  'show me my data in all lowercase
  while string$ <> "end"
    read string$
    print lower$(string$)
  wend
 
  data "Sally", "Sells", "Sea", "Shells", "By", "The", "Sea", "Shore"
[partTwo]
  data "Let's", "Do", "Only", "This", "A", "Second", "Time", "end"
 
  end
 
Reading DATA into Arrays
DATA is READ into variables.  It cannot be READ directly into arrays.  To fill arrays with DATA items, first READ the item into a variable, then use that variable to fill an index of the array.
 
'wrong
read numericArray(1)
 
'correct:
read num1
numericArray(1) = num1
 
Error Handling
An attempt to read more DATA items than are contained in the DATA lists causes the program to halt with an error.  Notice that in the examples above, an "end" tag is placed in the DATA and when it is reached, the program stops READing DATA.  This is an excellent way to prevent errors from occuring.  If an end tag or flag of some sort is not used, be sure that other checks are in place to prevent the READ statement from trying to access more DATA items than are contained in the DATA statements.
 
See also: READ, RESTORE, DATA


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/