Previous | Contents | Index |
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) |
REDIM array_name (int_expr, int_expr...) ... OR REDIM array_name [( [int_expr TO] int_expr, [int_expr TO] int_expr... )] ... |
Example 5-19 REDIM statement |
---|
dim name$(2) input 'How many names': num redim name$(num) for i = 1 to num input 'Enter a name': name$(i) next i do print for i = 1 to num if name$(i) = '' then print i; ' '; 'empty slot' else print i; ' '; name$(i) end if next i print input 'How many names': num if _back or _exit then exit do redim name$(num) loop end How many names? 3 Enter a name? Tim Enter a name? Sammy Enter a name? Fred 1 Tim 2 Sammy 3 Fred How many names? 4 1 Tim 2 Sammy 3 Fred 4 empty slot How many names? exit |
The REDIM statement is used to change the size of an array.
REDIM redimensions arrays. REDIM can be used only on arrays that have already been dimensioned with the DIM statement. The REDIM statement has the same rules, options and limits as the DIM statement.
Arrays can be dynamically expanded as needed. If you REDIM a single dimension array or the first dimension of a multi-dimensioned array to a larger size, the old values are kept. If you REDIM any array to a smaller size or REDIM two or more dimensions in a multi-dimensioned array to a larger size, the old values are lost.
If your application depends on REDIM initializing all array values, change your code as follows:
Old Code: REDIM X(100) New Code: REDIM X(1) REDIM X(100) |
The REDIM X(1) forces all array values to be initialized by the second REDIM statement.
OPTION BASE [0 | 1] |
Previous | Next | Contents | Index |