Changing the value of a variable
You can change the value of a CL variable using the Change Variable (CHGVAR) command.
The value can be changed:
- To a constant, shown in this example:
CHGVAR VAR(&INVCMPLT) VALUE(0)
&INVCMPLT is set to 0.
You could also use this notation:
CHGVAR &INVCMPLT 0
- To the value of another variable, shown in this example:
CHGVAR VAR(&A) VALUE(&B)
&A is set to the value of the variable &B
You could also use this notation:
CHGVAR &A &B
- To the value of an expression after it is evaluated, shown in
the following example:
CHGVAR VAR(&A) VALUE(&A + 1)
The value of &A is increased by 1.
You could also use this notation:
CHGVAR &A (&A + 1)
- To the value produced by the built-in function %SST, shown in
the following example:
CHGVAR VAR(&A) VALUE(%SST(&B 1 5))
&A is set to the first five characters of the value of the variable &B
- To the value produced by the built-in function %SWITCH, shown
in the following example:
CHGVAR VAR(&A) VALUE(%SWITCH(0XX111X0))
&A is set to 1 if job switches 1 and 8 are 0 and job switches 4, 5 and 6 are 1; otherwise, &A is set to 0.
- To the value produced by the built-in function %BIN:
CHGVAR VAR(&A) VALUE(%BIN((%B 1 4))
The first four characters of variable &B are converted to the decimal equivalent and stored in variable &A.
- To the value produced by the built-in function
%CHECK:
CHGVAR VAR(&A) VALUE(%CHECK('0123456789' &B))
The value in variable &B is checked and the position of the leftmost character that is not a digit is stored in variable &A. If all the characters in variable &B are digits, a value of zero is stored in variable &A.
- To the value produced by the built-in function
%CHECKR:
CHGVAR VAR(&A) VALUE(%CHECKR('*' &B))
The value in variable &B is checked and the position of the rightmost character that is not an asterisk (*) is stored in variable &A. If all the characters in variable &B are asterisks, a value of zero is stored in variable &A.
- To the value produced by the built-in function
%SCAN:
CHGVAR VAR(&A) VALUE(%SCAN('.' &B))
The value in variable &B is scanned and the position of the leftmost period (.) character is stored in variable &A. If there are no period characters in variable &B, a value of zero is stored in variable &A.
- To the value produced by the built-in function
%TRIM:
CHGVAR VAR(&A) VALUE(%TRIM(&B '* '))
Leading and trailing asterisk (*) and blank characters in variable &B will be trimmed off and the resulting string will be stored in variable &A.
- To the value produced by the built-in function
%TRIML:
CHGVAR VAR(&A) VALUE(%TRIML(&B))
Leading blank characters in variable &B will be trimmed off and the resulting string will be stored in variable &A.
- To the value produced by the built-in function
%TRIMR:
CHGVAR VAR(&A) VALUE(%TRIMR(&B '*'))
Trailing asterisk (*) characters in variable &B will be trimmed off and the resulting string will be stored in variable &A.
- To the value produced by the built-in function
%CHAR:
CHGVAR VAR(&A) VALUE(%CHAR(&B))
Variable &B will be converted into character format and the resulting string will be stored in variable &A.
- To the value produced by the built-in function
%UPPER:
CHGVAR VAR(&A) VALUE(%UPPER(&B))
Lowercase letters in variable &B will be converted into uppercase letters and the resulting string will be stored in variable &A.
- To the value produced by the built-in function
%SIZE:
CHGVAR VAR(&A) VALUE(%SIZE(&B))
The number of bytes occupied by variable &B will be stored in variable &A.
The CHGVAR command can be used to retrieve and to change the local data area also. For example, the following commands blank out 10 bytes of the local data area and retrieve part of the local data area:
CHGVAR %SST(*LDA 1 10) ' '
CHGVAR &A %SST(*LDA 1 10)
The following table shows valid assignments to variables from values (literals or variables).
Logical value | Character value | Decimal value | Signed integer value | Unsigned integer value | |
---|---|---|---|---|---|
Logical variable | X | ||||
Character variable | X | X | X | X | X |
Decimal variable | X | X | X | X | |
Signed integer variable | X | X | X | X | |
Unsigned integer variable | X | X | X | X |
- When specifying a numeric value for a character variable, remember
the following:
- The value of the character variable is right-aligned and, if necessary, padded with leading zeros.
- The character variable must be long enough to contain a decimal point and a minus (-) sign, when necessary.
- When used, a minus (-) sign is placed in the leftmost position of the value.
For example, &A is a character variable to be changed to the value of the decimal variable &B. The length of &A is 6. The length of &B is 5 and decimal positions is 2. The current value of &B is
123
. The resulting value of &A is123.00
. - When specifying a character value for a numeric variable, remember
the following:
- The decimal point is determined by the placement of a decimal point in the character value. If the character value does not contain a decimal point, the decimal point is placed in the rightmost position of the value.
- The character value can contain a minus (-) sign or plus (+) sign immediately to the left of the value; no intervening blanks are allowed. If the character value has no sign, the value is assumed to be positive.
- If the character value contains more digits to the right of the
decimal point than can be contained in the numeric variable, the digits
are truncated if it is a decimal variable, or rounded if it is an
integer variable. If the excess digits are to the left of the decimal
point, they are not truncated and an error occurs.
For example, &C is a decimal variable to be changed to the value of the character variable &D. The length of &C is 5 with 2 decimal positions. The length of &D is 10 and its current value is +123.1bbbb (where b=blank). The resulting value of &C is
123.10
.