Domain: Variable access (variable attributes)
The SIZE( ) function requests the number of elements comprising the specified variable. SIZE( ) can be applied to arrays, lists and structures.
In conjunction with the NEXT-VARIABLE-NAME( ) function, the result of SIZE( ) can be used, for example, as a loop count when analyzing the layout of complex variables.
Format
SIZE( ) |
VARIABLE-NAME = string_expression |
Result type
INTEGER
Input parameters
VARIABLE-NAME = string_expression
Designates a variable (array, list or structure). The variable name must be enclosed in apostrophes if it is specified directly, i.e as a literal (see the following example and the example in the description of IS-DECLARED( )).
Result
Number of elements comprising the variable “string_expression”.
0
The value “0” is returned in the following cases:
“string_expression” does not contain an element.
“string_expression” does not designate a complex variable, or no complex variable with this name exists.
Error message
SDP1101 SYNTAX ERROR IN VARIABLE NAME
Example
In the current task, a global variable list VARLIST for the task has already been declared and initialized. In the current procedure, VARLIST can contain exactly 10 elements.
/IF SIZE('VARLIST') > 10 / WRITE-TEXT 'too many elements' / FOR I = *LIST(VARLIST) / SHOW-VARIABLE I / END-FOR / GOTO TOOMANY /ELSE / COUNT = SIZE('VARLIST') / WHILE COUNT < 10 / VARLIST = COUNT+1, WRITE-MODE = *EXTEND / COUNT = COUNT+1 / END-WHILE /END-IF ... /TOOMANY: “Error handling > 10 list elements” ...
The size of the list variable is checked in the first line. If it contains more than 10 elements, the contents of all elements are output in a FOR loop and the procedure is continued with the error handling procedure TOOMANY.
If VARLIST does not contain more than 10 elements, the ELSE branch of the IF block is executed. This is a WHILE loop which appends elements to VARLIST until VARLIST contains precisely 10 elements. The procedure is then continued with the command which follows the END-IF command.