READ [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation
HP Business BASIC/XL Reference Manual
READ
The READ statement assigns data from one or more DATA statements to
specified variables. It also assigns the value of one or more data items
in a file to one or more variables.
Syntax
{variable }{ [variable ]}
READ {read_for_loop}{,[read_for_loop]} READ #fnum [, rnum [, wnum]]
[; input_list]
Parameters
variable Variable reference; that is, a variable name, array
reference (one element or an entire array), or substring
reference (see "Referencing Variables" in chapter 3 for
syntax).
read_for_loop A FOR loop within a READ statement used to assign
individual datum to variables. See below for syntax and
an explanation.
fnum The file number that HP Business BASIC/XL uses to
identify the file. It is a numeric expression that
evaluates to a positive short integer.
rnum Record number, a numeric expression. If a file I/O
statement specifies rnum, it is direct; otherwise, it is
sequential
wnum Word number, a numeric expression. If a file I/O
statement specifies wnum, it is a direct word. Direct
word reads are allowed only with BASIC DATA files.
[{,} ]
input_list item [{;}item]...
Each item is a numeric or string variable, an array
reference, or a FOR clause. An array reference has the
syntax
array_name([*[,*]...])
with one asterisk per dimension or it does not have
asterisks. Not using asterisks specifies any number of
dimensions. Either format is legal, but the format
without asterisks is noncompilable.
A FOR clause has the syntax
(FOR num_var=num_expr3 TO num_expr4
[STEP num_expr5], input_list)
A sequential read must have an input_list.
If a direct read does not have an input_list, it is the
same as the POSITION #fnum;rnum statement. That is, it
positions the file at the beginning of record rnum.
When used with data files, the READ statement assigns one file datum to
one input item. It accesses its input items from left to right. It
reads BASIC DATA, binary, and ASCII files differently; see Table 4-14.
Table 4-14. Effect of File Type on READ Statement
-------------------------------------------------------------------------------------------------
| | | | |
| | BASIC DATA | Binary | ASCII |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| Sequential Read | Datum indicated by | Record indicated by | Record indicated by |
| Starts at | datum pointer. | record pointer | record pointer. |
| | | (possibly within | |
| | | unexhausted record). | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| And Reads | As many records as | As many records as | As many records as |
| | needed to satisfy | needed to satisfy | needed to satisfy |
| | input list. | input list. | input list. |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| Direct Read Starts at | Record rnum. | Record rnum. | Record rnum. |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| And Reads | As many records as | As many records as | As many records as |
| | needed to satisfy | needed to satisfy | needed to satisfy |
| | input list. | input list. | input list. |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| Direct Word Read | Word wnum of record | Not allowed. | Not allowed. |
| Starts at | rnum. | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| And Reads | As many records as | Not allowed. | Not allowed. |
| | needed to satisfy | | |
| | input list. | | |
| | | | |
-------------------------------------------------------------------------------------------------
When reading from a binary file, HP Business BASIC/XL does not convert
data to the types of the variables to which it assigns them. For
example, if a program tries to read decimal data that is in a binary file
into real variables, the numbers returned are incorrect.
Examples
10 DATA 12,13,14
20 DATA 15,16,17,18
30 READ A,B !A=12, B=13
40 READ C !C=14
99 END
After line Data pointer is at: In line:
--------------------------------------------------------------------------------------
20 12 10
30 14 10
40 15 20
If possible, a datum from a DATA statement is interpreted as the type of
data required by the variable into which it is read. If an underflow
occurs, the value zero is assigned to the variable. Before a datum is
assigned to a variable, it is converted to the type of the variable, if
possible. A numeric variable requires a numeric literal, and a string
variable requires a string literal or any unquoted string. Numeric
literals are also unquoted string literals and can thus be assigned to a
string variable.
10 DATA 1234, "56", "seven", "eight", 12
20 READ N,A$ !N=1234, A$="56"
30 READ B$, C$ !B$="seven", C$="eight"
40 READ D$ !D$="12"
99 END
Specification of a substring of a string variable does not always "use
up" the value that is read into it. However, following the READ, the
data pointer moves to the next datum anyway. The rules of substring
assignment apply to READ.
10 DIM Str$[3], Str_array$(1:5)[6]
20 DATA Anteater, Bear, Cat, Dog
30 READ Str$[1:3] !Str$="Ant"
40 READ Str_array$(1)[1,2], Str_array$(2)[1;1] ! Str_array$(1)="Be",
99 END ! Str_array$(2)="C"
The READ statement assigns values from left to right when multiple
variables are specified. Thus, variable subscripts can be assigned just
prior to assignment to an array element. For example, in the statement
2450 READ X,Y,A(X,Y)
values are assigned to X and Y before the subscripts of the A array are
evaluated.
An example of using READ statements with data files:
100 READ #1; A,B,C
110 READ #2,5; D$,E
120 READ #3,7,4; F(),G$(*,*)
130 READ #4; N,M,(FOR I=1 TO 5, A(I,I), B$(I,I))
FOR Loops in READ statements
The READ statements in the previous examples have contained only
references to individual variables. A READ statement can contain a FOR
loop designed to assign values to specific array elements or substrings
of a string variable.
Syntax
(FOR num_var=num_expr1 TO num_expr2 [STEP num_expr3], input_list)
Parameters
num_var The numeric loop control variable that assumes the
values num_expr1, num_expr1+num_expr3,
num_expr1+(2*num_expr3) on successive executions of the
loop body.
num_expr1 The initial value that num_var is assigned.
num_expr2 Value to which num_var is compared before the loop body
is executed.
num_expr3 Amount by which num_var is incremented or decremented.
The default is one.
input_list The list of items to be read. This is the same as for
the READ statement without the FOR loop.
A READ statement executes the following steps each FOR loop specified:
1. num_var = num_expr1
2. If num_expr3 is positive; go to step 3 else num_expr3 is negative
got to step 4.
3. If num_var <= num_expr2, then assign data to the input_list
elements, and go to step 5; otherwise, stop.
4. If num_var >= num_expr2, then assign data to the input_list
elements, and go to step 5; otherwise, stop.
5. num_var=num_var+num_expr3.
6. Return to step 3 or 4 if num_expr3 is positive or negative,
respectively.
Examples
A variable specified within a FOR loop in a READ statement must contain a
reference to num_expr1 as a subscript or substring if the data are not to
be repeatedly assigned to the same variable or array element. When
100 READ (FOR I=1 TO 4 STEP 1 A(I))
is executed, the index I assumes the values 1, 2, 3, and 4 and assigns
the data to the array elements A(1), A(2), A(3), and A(4). The statement
200 READ (FOR I=2 to 6 STEP 2, A$[I;1])
assigns the first character in each of the next three double-quoted
string literal data items to positions 2, 4 and 6 in A$.
FOR loops within READ statements can be nested; for example, the
following statement reads data into a 3-by-5 array.
250 READ (FOR I=1 TO 3, (FOR J=1 TO 5, A(I,J))
MPE/iX 5.0 Documentation