4 Data Variables and Data handling

Declaring and Dimensioning Variables

Six variable declarative statements are available to dimension arrays and strings and declare the precision of numeric variables:

COM

DIM

INTEGER

DINTEGER

SHORT

REAL

They can be placed anywhere in a program. The size (number of dimensions and bounds of each dimension) of the array, which is specified, is known as the physical or maximum size. A new working size can be specified for the array, which cannot be greater than the total number of elements of the physical size. This can be done using a REDIM statement. The working size refers to the total number of elements being used. An array identifier, consisting of the array name and *, can be used to refer to all elements in the working size.

The OPTION BASE Statement

When dimensioning arrays, you may want to specify that the default lower bound be 1 rather than 0. This is done using the OPTION BASE statement:

OPTION BASE 1

This statement must come before any of the variable declarative statements used in a program. Then any lower bound not specified is 1. (Explicitly defining a lower bound for an array always over-rules an OPTION BASE statement.)

If OPTION BASE 1 is not declared in a program, you may wish to include the statement:

OPTION BASE 0

for documentation purposes.

The OPTION BASE statement cannot be executed from the keyboard.

The DIM Statement

The DIM (dimension) statement is used to dimension and reserve memory for real-precision numeric arrays and initialize each element to 0. It is also used to dimension and reserve storage space for simple strings and string arrays. Syntax for the DIM statement is as follows:

DIM item1 [,item2 . . . ]

DIM [instance : type_name]

DIM [instance AS type_name]

Each item can be one of the following:

For example:

10   OPTION BASE 1
20   DIM A(4,4),B$[56],C$(2,5),D$(10,10)[30],E(-5;5,-5;5)
Line 20 dimensions array A to be of 16 elements maximum. (The elements are REAL numeric precision, the default.) B$ is dimensioned as a simple string of 56 characters maximum; C$ is a string array of ten 18-character strings maximum (the default maximum length for strings); D$ is a string array having one hundred 30-character strings; and E is a real numeric array of 100 elements.

The maximum number of characters that may be specified for a simple string (or string array element) is 32767. This size may be limited by the memory available.

Note that in a DIM statement the subscripts must be explicitly quoted; it is not possible to use the default maximum array or string size.

User Defined Type example:

DIM Vehicle AS Tvehicle
DIM Vehicle:Tvehicle
The type has to be defined before the variable can be dimensioned, see chapter , User defined Types.

The NEW statement

The DIM statement is executed during the prerun of the program, so the TYPE has to be known and defined in the programcode at starttime. This is not always possible with User Defined Types.

The NEW statement makes it possible to dimension a variable during runtime of the program.

Syntax:

NEW instance : type_name

NEW instance AS type_name

It is possible to create a variable form an already existing one:

Syntax:

NEW STRUCT A = B

The variable A has the same type as the variable B, after executing this statement. The contant of variable B is not copied to A.

The INTEGER Statement

The INTEGER statement is used to dimension and reserve memory for integer-precision variables.

INTEGER numeric variable1 [(subscripts)] [,num variable2 [(subscripts)]. . .]

For example:

40   INTEGER X,Y(2,2)
declares a simple integer X and an integer array Y.

The DINTEGER Statement

DINTEGER numeric variable1 [(subscripts)] [,num variable2 [(subscripts)]. . .]

declares a double integer variable. (see also INTEGER above.)

The SHORT Statement

The SHORT statement is used to dimension and reserve storage for short-precision variables. Syntax is as follows:

SHORT numeric variable1 [(subscripts)] [,num variable2 [(subscripts)]. . .]

For example:

50   SHORT A(4,5,6),B(3,2,1),D
declares A and B as short-precision arrays and D as a simple, short precision variable.

The REAL Statement

The REAL statement is used to dimension and reserve memory for real-precision variables. Syntax is as follows:

REAL numeric variable1 [(subscripts)] [,num variable2 [(subscripts)]. . .]

For example:

60   REAL M(2,3,4,5),N
dimensions the array M and simple variable N.

The COM Statement

The COM (common) statement is used to dimension and reserve memory for simple, array and user defined type variables. This includes strings, all four numeric precisions and user defined types. COM is unique because it reserves memory space in a special common area which allows data to be transferred to subprograms or to other programs. Of course, data may always be transferred to subprograms using parameters. The COM statement is useful when you wish to share data among many programs. The syntax is as follows:

COM item1 [,item2 . . . ]

COM [instance : type_name]

COM [instance AS type_name]

Each item can be one of the following:

In addition, any one of the keywords INTEGER, DINTEGER, SHORT, and REAL may precede one or more numeric variables.

For example:

70  COM A,B(2,4),C$,#3,INTEGER E,F$(5)[24],G,SHORT H(5),I,DINTEGER D1,D2
The variables A,B(2,4) and G are real precision. Real precision is assumed at the beginning of the COM list and for numeric variables declared after any string. All variables following a numeric precision keyword have that precision until another type is specified or a string is declared. Thus both H(5) and I are short precision. The #3 item allows passing an assigned data file of the same number to another program or subprogram. For example:

Main Program                       Overlay

10  COM A,B,#1                     200  COM A,B,#5
20  ASSIGN #1 TO "Data"
30  CALL Data_prog
.
.
10  SUB Data prog                  Start of Subprogram
20  COM C,D,#3
.
.
The file number item in line 10 allows the file assigned in line 20 to remain assigned in the subprogram (as file #3) and in the overlayed program (as file #5). COM may occur anywhere in each program and may be edited.

The names of variables in corresponding COM statements need not match. But all items must be of the same type and be in the same order. Arrays must have the same number of dimensions and elements. Once a string is dimensioned in common, it is automatically dimensioned to the same size in all subsequent subprograms.

COM statements in separate programs need not have the same number of items. You need only quote the items that other programs or overlays need. A second (or further) COM statement in the main program will, if shorter, cause the omitted items to be lost or the extra files to be closed. If the succeeding COM list is longer, the new items will be dimensioned and initialized.

EXAMPLE OF TYPE USED AS GLOBAL VARIABLE

Other Features of Variable Declarative Statements

DIM, COM, INTEGER, DINTEGER, SHORT, and REAL statements are programmable only. They may appear anywhere in a program but they must not precede an OPTION BASE statement. (It is recommended that they be placed near the start of a program; clearly defined variables make a program easier to read.)

At pre-run initialization, all variables declared in DIM, SHORT, INTEGER, DINTEGER, and REAL are dimensioned and initialized. ("Initialization" means that numeric variables are set to 0 and string variables to the null string.)

DIM need not be used to assign space for strings with 18 character or less or for arrays having upper bounds of ten or

less. These can be dimensioned implicitly. (They will be set to the default--18 characters per string and 10 elements per dimension.)

A program can have more than one DIM, SHORT, INTEGER, DINTEGER or REAL statement, but the same variable name can be declared only once in a program segment. The same name, however, may be used for a simple numeric, simple string, numeric array and string array. For instance:

10 OPTION BASE 1
20 DIM A(5,5),A$[50],A$(10)[80]
These variable names are legal, although confusing.


Eloquence Language Manual - 19 DEC 2002