READ

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

The READ command "READs" string or numerical data one item at a time from items listed in DATA statements.

Syntax

  1. read text$
  2. read number
  3. read text1$, text2$, ...
  4. read number1, number2, ...
  5. read text$, number, ...

Hints

The data which is READ is assigned to a variable that the programmer provides. The variable type must match the data type which is being READ. When the READ command is issued, it will read as many data items as there are variables to fill.
Subsequent READ statements will begin reading data from the point where the last READ statement ended. In other words, if the DATA statements total 10 elements or items, and the first READ command reads the first 4 items, the next READ statement encountered will start reading the data at item number 5.
To "reset" the data and begin reading the data from the first item once again, the RESTORE command is issued.
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.

Example

' read some names into the program
    data "George", "Phil", "Pete", "Sam"
    print "First 2 names"
    for i = 1 to 2         'read first 2 names from first data statement
        read name$
        print name$
    next i
    print "Next 2 names"
    for x = 3 to 4         'read next 2 names from first data statement
        read name$
        print name$
    next x
    print "Next 3 names"
    for y = 1 to 3         'read 3 names from data statements at end of listing
        read name$
        print name$
    next y
    data "Joe", "Fred"     'loop y = 1 to 3 will read Joe then Fred then Joan
    data "Joan"
    end

More Examples

' read data from which your user may choose
    print "Warrior! Pick your weapon!"

' listing the data statements at beginning of listing
    data "Knife", "Saber", "Spear", "Rubber band"

    print "Select the number of your choice"

    read item$
    print "1 - "; item$
    read item$
    print "2 - "; item$
    read item$
    print "3 - "; item$
    read item$
    print "4 - "; item$
    print "Your choice?"
    end

' reading data using a loop
    print "Warrior! Pick your weapon!"
    print "Select the number of your choice"

    for i = 1 to 4
        read item$
        print i;" - "; item$
    next i
    print "Your choice?"

' Here the data statements are at end of listing
    data "Knife", "Saber", "Spear", "Rubber band"
    end
' An example of reading literal numbers into a string variable:
    data "Jim", 27
    read name$, age$
    print name$; " is ";age$; " years old"
    end

Useful Procedures

' Place a useful function or sub using this keyword here