| Previous | Contents | Index |
| Example 5-13 DATA, READ, RESTORE Statements |
|---|
dim months$(6)
data January, February, March
data April, May, June
for i = 1 to 6
read months$(i)
print months$(i)
next i
end
January
February
March
April
May
June
|
DATA and READ statements are used to assign data to variables in cases where the data will not change with successive runs of the program.
The DATA and READ statements assign data to variables. DATA specifies a list of data to assign. The data must be given as constants and can be string, numeric or integer types. Multiple data items must be separated by commas.
The READ statement specifies a list of variables to assign data to. The variables can be string, numeric or integer variables. They can be substrings, array elements, etc..
When SheerPower executes the first READ statement, it goes to the first DATA statement and assigns the items in the DATA list to the variables in the READ list. The first variable in the READ list is assigned the first value in the DATA list. The second variable in the READ list is assigned the second value in the DATA list, and so on.
DATA constant, constant, constant, constant...
.
. | | | |
.
READ variable, variable, variable, variable...
|
If the data item contains a comma, the data item should be enclosed with single or double quotes. For example:
| Example 5-14 DATA items containing commas |
|---|
dim amounts$(3) data '$25,000', '$250,000', '$2,500,000' read amounts$(1), amounts$(2), amounts$(3) print amounts$(1), amounts$(2), amounts$(3) end $25,000 $250,000 $2,500,000 |
The variable types and data types must match or an exception will result. For example, if the third item in the DATA list is a string constant, and the third variable in the READ list is a numeric variable, an exception will result.
When the second READ statement is executed, SheerPower starts reading from the first unread data item in the DATA list. For example:
| Example 5-15 DATA and READ statements |
|---|
dim months$(4) data January, February, March, April, May, June read months$(1), months$(2) read months$(3), months$(4) print months$(1), months$(2), months$(3), months$(4) end January February March April |
In the example above, when the first READ statement is executed, SheerPower reads the months January and February. When the second READ statement is executed, SheerPower will continue at the first unread month---March---and read it into months$(3).
If you attempt to read more data than exists; that is, if your READ list has more items than your DATA list, an exception will result. You can avoid this by using the RESTORE statement to restore the DATA list and read from the beginning again.
The READ and DATA statements must occur in the same program unit. For example, you cannot not have your DATA statements in the main program unit and your matching READ statements in a subprogram.
See Section 5.6.2 for information on using RESTORE.
RESTORE
|
| Example 5-16 RESTORE statement |
|---|
dim months$(3)
dim more_months$(3)
data January, February, March
for i = 1 to 3
read months$(i)
print months$(i)
next i
restore
print
for i = 1 to 3
read more_months$(i)
print more_months$(i)
next i
end
January
February
March
January
February
March
|
RESTORE is used to access the same set of data (from a DATA statement) for a number of READ statements.
RESTORE restores the DATA statements in a program unit so they can be used again. When the RESTORE statement is executed, all the DATA statements which have been read are restored. The next READ statement causes SheerPower to go back to the first DATA statement and begin assigning the items in its list.
In the example program, the months will be read and assigned to the array MONTHS$. When the RESTORE is executed, the DATA statements will be restored. When the READ statement is executed, the months will be read into the new array MORE_MONTHS$.
ROUTINE routine_name: PRIVATE var, var, var, ...
or
ROUTINE routine_name: PRIVATE INTEGER var, STRING var, STRING var, ...
|
| Example 5-17 Private variables in routines |
|---|
do_totals
end
routine do_totals: private mytotal, desc$
mytotal = 15
desc$ = 'Test Totals'
print desc$; mytotal
end routine
Test Totals 15
|
SheerPower allows you to use private variables in a routine. Private variables are variables identified with a specific routine. This option allows you to use the same variable names more than once because, internally, SheerPower prefixes the variables with the routine name and a "$" character.
In the above example, the private variables "mytotal" and "desc$" are
internally known to SheerPower as:
do_totals$mytotal
do_totals$desc$
From inside the routine, you can reference the variables by their private names. For example: mytotal, desc$
From outside the routine (or while debugging the code), you can reference the private variables by their internal known names. For example: do_totals$mytotal, do_totals$desc$
See Appendix M, SheerPower and Program Segmentation for more on routines and private routines in SheerPower. |
Arrays are a type of variable. They are used to store and manipulate tables of variable information. An array must be defined before it is used in a program. Array variables are described in Section 4.5.1, Arrays.
Arrays are dimensioned with a DIM statement. The REDIM statement can be used to redimension an array; that is, to change the dimensions of an array which has been defined with the DIM statement. The OPTION BASE statement changes the default low bound. By default, the low bound is 1.
About arrays:
DIM [INTEGER | REAL | STRING | BOOLEAN]
array_name ([int_expr TO] int_expr [, ...])
|
| Example 5-18 DIM statement |
|---|
dim name$(4)
for i = 1 to 4
input 'Enter a name': name$(i)
next i
print
for i = 1 to 4
print i; ' '; name$(i)
next i
end
Enter a name? Jim
Enter a name? Jane
Enter a name? Bob
Enter a name? Betty
1 Jim
2 Jane
3 Bob
4 Betty
|
DIM is used to dimension arrays. Arrays are used to store tables of variable information. An array must be dimensioned before it can be used.
The simplest version of a DIM statement is:
DIM array_name(int_expr)
|
array_name is the name of the array being defined. The array name must meet the rules for variable names. int_expr is the high bound for the array---the highest element allowed in a dimension. The low bound is the lowest element allowed in a dimension. The low bound defaults to 1. For example:
DIM NAME$(4)
|
This statement defines a one-dimensional array with four elements:
NAME$(1) NAME$(2) NAME$(3) NAME$(4) |
Multiple Dimensions
An array can have up to 32 dimensions. A high bound must be specified for each dimension.
DIM array_name(int_expr [, int_expr, ...])
|
For example:
dim name$(4,2)
|
This statement defines the following two-dimensional array:
NAME$(1,1) NAME$(1,2) NAME$(2,1) NAME$(2,2) NAME$(3,1) NAME$(3,2) NAME$(4,1) NAME$(4,2) |
Low Bounds
The low bound is the lowest element a dimension can have. Low bounds can be specified for each dimension of an array. If no low bound is specified, the default is 1. To specify a low bound, use the following format:
DIM array_name (int_ expr TO int_expr)
|
The number preceding TO is the low bound. For example:
dim name$(4,18 to 20)
|
This statement creates an array whose first dimension contains elements 1-4 and whose second dimension contains elements 18-20:
NAME$(1,18) NAME$(1,19) NAME$(1,20) NAME$(2,18) NAME$(2,19) NAME$(2,20) NAME$(3,18) NAME$(3,19) NAME$(3,20) NAME$(4,18) NAME$(4,19) NAME$(4,20) |
| Previous | Next | Contents | Index |