變更變數值

您可以使用 Change Variable (CHGVAR) 指令來變更 CL 變數的值。

可以變更值:

  • 至常數,如下列範例所示:
    
    CHGVAR   VAR(&INVCMPLT)   VALUE(0)
    

    &INVCMPLT is set to 0.

    您也可以使用此表示法:

    
    CHGVAR   &INVCMPLT  0
    
  • 變更為另一個變數的值,如下列範例所示:
    
    CHGVAR   VAR(&A)   VALUE(&B)
    

    &A is set to the value of the variable &B

    您也可以使用此表示法:

    
    CHGVAR   &A  &B
    
  • 評估表示式之後的表示式值,如下列範例所示:
    
    CHGVAR   VAR(&A)   VALUE(&A + 1)
    

    The value of &A is increased by 1.

    您也可以使用此表示法:

    
    CHGVAR   &A  (&A + 1)
    
  • 至內建函數 %SST 所產生的值,如下列範例所示:
    
    CHGVAR   VAR(&A)   VALUE(%SST(&B 1 5))
    

    &A is set to the first five characters of the value of the variable &B

  • 至內建函數 %SWITCH 所產生的值,如下列範例所示:
    
    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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %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.

  • 至內建函數 %UPER 所產生的值:
    
    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.

  • 至內建函數 %SIZE 所產生的值:
    
    CHGVAR  VAR(&A)  VALUE(%SIZE(&B))
    

    The number of bytes occupied by variable &B will be stored in variable &A.

  • 至內建函數 %PARMS 所產生的值:
    CHGVAR  VAR(&A)  VALUE(%PARMS())
    

    The number of parameters that were passed to the program will be stored in variable &A.

CHGVAR 指令可用來擷取及變更區域資料區。 例如,下列指令會清空本端資料區的 10 個位元組,並擷取部分本端資料區:


CHGVAR %SST(*LDA 1 10) ' '
 
CHGVAR &A %SST(*LDA 1 10)

下表顯示從值 (文字或變數) 到變數的有效指派。

表 1. 值中變數的有效指派
  邏輯值 字元值 小數值 帶正負號的整數值 不帶正負號的整數值
邏輯變數 X        
字元變數 (character variable) X X X X X
十進位變數   X X X X
帶正負號的整數變數   X X X X
不帶正負號的整數變數   X X X X
附註:
  1. 指定字元變數的數值時,請記住下列:
    • 字元變數的值會向右對齊,必要的話,會以前導零填補。
    • 必要時,字元變數必須足夠長,才能包含小數點及減號 (-)。
    • 使用時,減號 (-) 會放置在值最左側的位置。

    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 is 123.00.

  2. 當指定數值變數的字元值時,請記住下列各項:
    • 小數點是由字元值中小數點的位置來決定。 如果字元值不包含小數點,則小數點會放在值的最右邊位置。
    • 字元值可以包含緊接在值左側的減號 (-) 或加號 (+); 不容許中間的空白。 如果字元值沒有正負號,則會假設該值是正數。
    • 如果字元值包含小數點右側的位數超過數值變數中可包含的位數,則會截斷數字 (如果它是十進位變數) ,或捨入 (如果它是整數變數)。 如果超出的位數位於小數點左側,則不會截斷它們,且會發生錯誤。

      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.