Built-In Functions in CL

145 Views


 %ADDRESS built-in function

The address built-in function (%ADDRESS or %ADDR) can be used to change or test the memory address stored in a CL pointer variable and can only be used within a CL program or procedure.

In a CHGVAR command, you can specify the %ADDRESS function to change the value of a pointer variable.

On the IF command, %ADDRESS function can be specified on the COND parameter to check the value stored in a pointer variable. The format of the address built-in function is:

%ADDRESS(variable name)

or

%ADDR(variable name)

In the following example, pointer variable &P1 is initialized to the address of the first byte of character variable &C1. Later in the procedure, the pointer is checked using the %ADDRESS function to see if it still points to &C1 and, if not, resets &P1 to the first byte of CL variable &C1 using the %ADDRESS function.

PGM
DCL &C1 *CHAR 10
DCL &P1 *PTR ADDRESS(&C1)
:
IF COND(&P1 *NE %ADDRESS(&C1)) +
 THEN(CHGVAR &P1 %ADDRESS(&C1))
:
ENDPGM

%BINARY built-in function

The binary built-in function (%BINARY or %BIN) interprets the contents of a specified CL character variable as a signed binary integer.

The starting position begins at the position specified and continues for a length of 2 or 4 characters.

The syntax of the binary built-in function is shown in the following example:

%BINARY(character-variable-name starting-position length)
or
%BIN(character-variable-name starting-position length)

The starting position and length are optional.

A 2-byte character variable can hold signed binary integer values from -32 768 through 32 767.

A 4-byte character variable can hold signed binary integer values from -2 147 483 648 through 2 147 483 647.

The following examples are about the binary built-in function:

DCL VAR(&B2) TYPE(*CHAR) LEN(2) VALUE(Xā€™001Cā€™)
DCL VAR(&N) TYPE(*DEC) LEN(3 0)
CHGVAR &N %BINARY(&B2)

The contents of variable &B2 is treated as a 2-byte signed binary integer and converted to its decimal equivalent of 28. It is then assigned to the decimal variable &N.

DCL VAR(&N) TYPE(*DEC) LEN(5 0) VALUE(107)
DCL VAR(&B4) TYPE(*CHAR) LEN(4)
CHGVAR %BIN(&B4) &N

The value of the decimal variable &N is converted to a 4-byte signed binary number and is placed in character variable &B4. Variable &B4 will have the value of X'0000006B'.

DCL VAR(&X) TYPE(*CHAR) LEN(50)
CHGVAR %BINARY(&X 15 2) VALUE(122.56)

 

%CHAR built-in function

%CHAR converts logical, decimal, integer, or unsigned integer data to character format.

The converted value can be assigned to a CL variable, passed as a character constant to another program or procedure, or specified as a value for a command parameter of a CL command run from compiled CL.

The format of the convert to character data built-in function is:

%CHAR(convert-argument)

The convert-argument must be a CL variable with TYPE of *LGL, *DEC, *INT or *UINT.

For example, %CHAR of a CL variable declared with TYPE(*DEC) and LEN(7,3) CL variable might return the value '-1.234'. 

The following are examples of using the %CHAR built-in function:

• Convert packed decimal variable

DCL VAR(&MSG) TYPE(*CHAR) LEN(26)
DCL VAR(&ANSWER) TYPE(*DEC) LEN(10 5) VALUE(-54321.09876)
/* &MSG will have the value 'The answer is -54321.09876' */
CHGVAR VAR(&MSG) VALUE('The answer is' *BCAT %CHAR(&ANSWER))

• Convert integer or unsigned integer variable

DCL VAR(&MSG) TYPE(*CHAR) LEN(60)
DCL VAR(&LENGTH) TYPE(*UINT) LEN(2) VALUE(50)
DCL VAR(&TEMP) TYPE(*INT) LEN(2) VALUE(-10)
/* &MSG will have the value 'The length of this box is 50 centi+
meters. ' */
CHGVAR VAR(&MSG) VALUE('The length of this box is' *BCAT %CHAR(&LENGTH) +
*BCAT 'centimeters.')
/* &MSG will have the value 'The temperature dropped to -10 degrees +
Centigrade. ' */
CHGVAR VAR(&MSG) VALUE('The temperature dropped to' *BCAT %CHAR(&TEMP) +
*BCAT 'degrees Centigrade.')

• Convert logical variable

DCL VAR(&ENDOFFILE) TYPE(*LGL) VALUE('1')
SNDPGMMSG MSG('End of file?' *BCAT %CHAR(&ENDOFFILE))

• Leading zeros will be suppressed but all decimal position zeros will be kept

DCL &AMOUNT TYPE(*DEC) LEN(7 4) VALUE(099.5)
/* &MSG will have the value "Your amount comes to 99.5000" */
SNDPGMMSG MSG('Your amount comes to' *BCAT %CHAR(&AMOUNT))

• Decimal format and zero suppression

DCL VAR(&MSG) TYPE(*CHAR) LEN(20)
DCL VAR(&ANSWER) TYPE(*DEC) LEN(10 2) VALUE(-0.45)
/* &MSG will have the value 'The answer is -.45 ', while +
CHGJOB DECFMT(*BLANK) */
/* &MSG will have the value 'The answer is -0,45 ', while CHGJOB DECFMT(J) */
/* &MSG will have the value 'The answer is -,45 ', while CHGJOB DECFMT(I) */
CHGVAR VAR(&MSG) VALUE('The answer is' *BCAT %CHAR(&ANSWER))

 

%CHECK built-in function

The check built-in function (%CHECK) returns the first position of a base string that contains a character that does not appear in the comparator string. If all of the characters in the base string also appear in the comparator string, the function returns 0.

The format of the check built-in function is:

%CHECK(comparator-string base-string [starting-position])

The following examples are about the check built-in function:

• Check for any characters that are not digits (0-9). Here the comparator string is a character literal that contains the character digits 0 through 9. If the CL variable &SN contains any characters that are not digits, a message is sent.

PGM PARM(&SN)
DCL VAR(&SN) TYPE(*CHAR) LEN(10) 
IF COND(%CHECK('0123456789' &SN) *NE 0) +
 THEN(SNDPGMMSG ('INVALID CHARACTER FOUND!'))

• Find the first character that is not a dollar sign or an asterisk. The characters in variable &PRICE are checked from left to right. Since the first character that is not a dollar sign or asterisk is the eighth character, the value 8 is assigned to CL variable &POS by the CHGVAR command.

DCL VAR(&PRICE) TYPE(*CHAR) VALUE('$******5.27*** ')
DCL VAR(&COMP) TYPE(*CHAR) LEN(2) VALUE('$*ā€™)
DCL VAR(&POS) TYPE(*UINT) LEN(2)
CHGVAR VAR(&POS) VALUE(%CHECK(&COMP &PRICE))

• Check if any characters are not digits, starting from a specific position in the string. The characters in CL variable &SN are checked starting with the fifth byte. If there are characters in bytes 5 through 10 of &SN that are not digits, a message is sent.

PGM PARM(&SN)
DCL VAR(&SN) TYPE(*CHAR) LEN(10)
DCL VAR(&POS) TYPE(*UINT) LEN(2) VALUE(5)
IF COND(%CHECK('0123456789' &SN &POS) *NE 0) +
 THEN(SNDPGMMSG ('INVALID CHARACTER FOUND!'))

• Check characters from the local data area (*LDA). The position of the leftmost byte in the local data area (LDA) that is not a character digit is assigned to CL variable &POS. If all 1024 characters of the LDA are character digits. a value of zero is assigned to variable &POS.

DCL VAR(&POS) TYPE(*UINT) LEN(2)
CHGVAR VAR(&POS) VALUE(%CHECK('0123456789' *LDA))

 

%CHECKR built-in function

The reverse check built-in function (%CHECKR) returns the last position of a base string that contains a character that does not appear in the comparator string.

If all of the characters in the base string also appear in the comparator string, the function returns 0.

The format of the reverse check built-in function is:

%CHECKR(comparator-string base-string [starting-position])

The following examples are about the reverse check built-in function:

• Retrieve a numeric value from a string. The first CHGVAR uses the %CHECK built-in function to find the leftmost character that is not a dollar sign ($) or an asterisk (*) or a blank. The second CHGVAR uses the %CHECKR built-in function to find the rightmost character that is not a dollar sign, asterisk, or blank. The third CHGVAR computes the number of characters between the two values and the last CHGVAR uses the substring (%SST) built-in function to convert part of the base string to a decimal CL variable

DCL VAR(&PRICE) TYPE(*CHAR) VALUE('$******5.27*** ')
DCL VAR(&COMP) TYPE(*CHAR) LEN(3) VALUE('$* ā€™)
DCL VAR(&SPOS) TYPE(*UINT) LEN(2)
DCL VAR(&EPOS) TYPE(*UINT) LEN(2)
DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)
DCL VAR(&LEN) TYPE(*UINT) LEN(2)
CHGVAR VAR(&SPOS) VALUE(%CHECK(&COMP &PRICE))
CHGVAR VAR(&EPOS) VALUE(%CHECKR(&COMP &PRICE))
CHGVAR VAR(&LEN) VALUE(&EPOS - &SPOS + 1)
CHGVAR VAR(&DEC) VALUE(%SST(&PRICE &SPOS &LEN))

• Reverse check if any characters are not digits, starting from a specific position in the string. The characters in CL variable &SN are checked, from right to left, starting with the ninth byte. If there are characters in bytes 9 through 1 of &SN that are not digits, a message is sent.

PGM PARM(&SN)
DCL VAR(&SN) TYPE(*CHAR) LEN(10)
DCL VAR(&POS) TYPE(*UINT) LEN(2) VALUE(9)
IF COND(%CHECKR('0123456789' &SN &POS) *NE 0) +
 THEN(SNDPGMMSG ('INVALID CHARACTER FOUND!'))

• Reverse check characters from the local data area (*LDA). The position of the rightmost byte in the local data area (LDA) that is not a character digit is assigned to CL variable &POS. If all 1024 characters of the LDA are character digits. a value of zero is assigned to variable &POS.

DCL VAR(&POS) TYPE(*UINT) LEN(2)
CHGVAR VAR(&POS) VALUE(%CHECKR('0123456789' *LDA))

 

%DEC built-in function

%DEC converts character, logical, decimal, integer, or unsigned integer data to packed decimal format.

The converted value can be assigned to a CL variable, passed as a numeric constant to another program or procedure, or specified as a value for a command parameter of a CL command run from compiled CL.

The format of the convert to packed decimal data built-in function is:

%DEC(convert-argument [total-digits decimal-places])

The following are examples of using the %DEC built-in function:

• Convert character variable

DCL VAR(&POINTS) TYPE(*CHAR) LEN(10) VALUE('-123.45')
DCL VAR(&ANSWER) TYPE(*DEC) LEN(10 5)
/* &ANSWER will have the value -00023.45000 */
CHGVAR VAR(&ANSWER) VALUE(100 + %DEC(&POINTS 5 2))

• Convert logical variable

DCL VAR(&ANSWER1) TYPE(*LGL) VALUE('1')
DCL VAR(&ANSWER2) TYPE(*LGL) VALUE('1')
DCL VAR(&NUM) TYPE(*DEC) LEN(5 0)
/* &NUM will have the value 00002. */
CHGVAR VAR(&NUM) VALUE(%DEC(&ANSWER1 1 0) + %DEC(&ANSWER2))
SNDPGMMSG MSG('The number of YES answers is' *BCAT %CHAR(&NUM))

• Convert packed decimal variable

DCL VAR(&POINTS1) TYPE(*DEC) LEN(5 2) VALUE(100.23)
DCL VAR(&POINTS2) TYPE(*DEC) LEN(5 2) VALUE(100.45)
IF (%DEC(&POINTS1 3 0) *EQ %DEC(&POINTS2 3 0)) +
THEN(SNDPGMMSG ('The scores are the same!'))

• Convert integer or unsigned integer variable

DCL VAR(&P1) TYPE(*INT) LEN(2) VALUE(-1)
DCL VAR(&P2) TYPE(*UINT) LEN(2) VALUE(1)
DCL VAR(&NUM) TYPE(*DEC) LEN(5 0)
/* &NUM will have the value 00000. */
CHGVAR VAR(&NUM) VALUE(%DEC(&P1 10 0)+%DEC(&P2 5 2))

• Extra decimal digits will be truncated without rounding

DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('-123.567')
DCL VAR(&VALUE) TYPE(*DEC) LEN(7 2)
/* &VALUE will have the value -00123.56 */
CHGVAR VAR(&VALUE) VALUE(%DEC(&STRING 7 2))

• All-blank value will return a zero value

DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE(' ')
DCL VAR(&VALUE) TYPE(*DEC) LEN(7 2)
/* &VALUE will have the value 00000.00 */
CHGVAR VAR(&VALUE) VALUE(%DEC(&STRING 7 2))

 

%INT built-in function

%INT converts character, logical, decimal, or unsigned integer data to integer format.

The converted value can be assigned to a CL variable, passed as a numeric constant to another program or procedure, or specified as a value for a command parameter of a CL command run from compiled CL.

The %INT built-in function can be used anywhere that CL supports an arithmetic expression.

The format of the convert to integer data built-in function is:

%INT(convert-argument)

The following are examples of using the %INT built-in function:

• Convert character variable

DCL VAR(&POINTS) TYPE(*CHAR) LEN(10) VALUE('-123.45')
DCL VAR(&ANSWER) TYPE(*INT)
/* &ANSWER will have the value -23 */
CHGVAR VAR(&ANSWER) VALUE(100 + %INT(&POINTS))

• Convert logical variable

DCL VAR(&ANSWER1) TYPE(*LGL) VALUE('1')
DCL VAR(&ANSWER2) TYPE(*LGL) VALUE('1')
DCL VAR(&NUM) TYPE(*INT)
/* &NUM will have the value 2. */
CHGVAR VAR(&NUM) VALUE(%INT(&ANSWER1) + %INT(&ANSWER2))
SNDPGMMSG MSG('The number of YES is' *BCAT %CHAR(&NUM))

• Convert packed decimal variable

DCL VAR(&POINTS1) TYPE(*DEC) LEN(5 2) VALUE(100.23)
DCL VAR(&POINTS2) TYPE(*DEC) LEN(5 2) VALUE(100.45)
IF (%INT(&POINTS1) *EQ %INT(&POINTS2)) +
THEN(SNDPGMMSG ('The scores are the same!'))

• Convert unsigned integer variable

DCL VAR(&P1) TYPE(*INT) LEN(2)
DCL VAR(&P2) TYPE(*UINT) LEN(2) VALUE(1)
CHGVAR VAR(&P1) VALUE(%INT(&P2))

• Decimal digits will be truncated without rounding

DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('-123.9')
DCL VAR(&ANSWER) TYPE(*INT)
/* &ANSWER will have the value -123 */
CHGVAR VAR(&ANSWER) VALUE(%INT(&STRING))

 

%LEN built-in function

The %LEN built-in function returns the number of digits or characters of the CL numeric or character variable.

The format of the getting length built-in function is: %LEN(variable-argument)

The following are examples of using the %LEN built-in function:

• Get the length of numeric variables

DCL VAR(&NUM1) TYPE(*DEC)
DCL VAR(&NUM2) TYPE(*DEC) LEN(7 2)
DCL VAR(&NUM3) TYPE(*INT) LEN(4)
DCL VAR(&NUM4) TYPE(*UINT) LEN(2)
DCL VAR(&RTN) TYPE(*INT) LEN(2)
/* &RTN will have the value 15. */
CHGVAR VAR(&RTN) VALUE(%LEN(&NUM1))
/* &RTN will have the value 7. */
CHGVAR VAR(&RTN) VALUE(%LEN(&NUM2))
/* &RTN will have the value 10. */
CHGVAR VAR(&RTN) VALUE(%LEN(&NUM3))
/* &RTN will have the value 5. */
CHGVAR VAR(&RTN) VALUE(%LEN(&NUM4))

• Get the length of character variables

DCL VAR(&CHAR1) TYPE(*CHAR)
DCL VAR(&CHAR2) TYPE(*CHAR) LEN(20)
DCL VAR(&RTN) TYPE(*INT) LEN(2)
/* &RTN will have the value 32. */
CHGVAR VAR(&RTN) VALUE(%LEN(&CHAR1))
/* &RTN will have the value 20. */
CHGVAR VAR(&RTN) VALUE(%LEN(&CHAR2))
/* &RTN will have the value 52. */
CHGVAR VAR(&RTN) VALUE(%LEN(&CHAR1) + %LEN(&CHAR2))

 

%LOWER built-in function

The %LOWER built-in function returns a character string that is the same length as the argument specified with each uppercase letter replaced by the corresponding lowercase letter.

The following are examples of using the %LOWER built-in function:

• Convert to lowercase

DCL VAR(&STR) TYPE(*CHAR) LEN(12) VALUE('Hello World!')
/* 'hello world!' is to be sent */
SNDPGMMSG (%LOWER(&STR))

• Convert to lowercase based on CCSID

/* define &STR as 'Hello World!' in CCSID 819 (ISO/ANSI Multilingual) */
DCL VAR(&STR) TYPE(*CHAR) LEN(12) +
 VALUE(X'48656C6C6F20576F726C6421')
/* &STR will have the value x'68656C6C6F20776F726C6421' ('hello world!') */
CHGVAR VAR(&STR) VALUE(%LOWER(&STR 819))

 

%OFFSET built-in function

The offset built-in function (%OFFSET or %OFS) can be used to store or change the offset portion of a CL pointer variable.

PGM
DCL &C1 *CHAR 30000
DCL &P1 *PTR
DCL &P2 *PTR
DCL &OFF1 *UINT 4
DCL &OFF2 *UINT 4
DCL &OFF3 *UINT 4
CHGVAR &OFF1 %OFFSET(&P1) /* 1 */
CHGVAR &P1 %ADDRESS(&C1) /* 2 */
CHGVAR &OFF2 %OFFSET(&P1) /* 3 */
CHGVAR &OFF3 (&OFF2+100) /* 4 */
236  IBM i: CL overview and concepts
CHGVAR %OFFSET(&P1) &OFF3 /* 5 */
CHGVAR %OFFSET(&P1) (%OFFSET(&P1)-20) /* 6 */
ENDPGM

The %OFFSET built-in function cannot be used with a pointer variable that addresses teraspace storage.


%SCAN built-in function

The scan built-in function (%SCAN) returns the first position of a search argument in the source string, or 0 if it was not found.

The format of the scan built-in function is: %SCAN(search-argument source-string [starting-position])

The following examples are about the scan built-in function:

• Search for a specific string, CL variable &FNAME is scanned for the string ’John’. If the string ’John’ is not found anywhere in variable &FNAME, a message is sent. 

The scan is case-sensitive, so a scan for ’John’ does not return a positive result if &FNAME contains the value ’JOHN

PGM PARM(&FNAME)
DCL VAR(&FNAME) TYPE(*CHAR) LEN(10)
IF COND(%SCAN('John' &FNAME) *EQ 0) +
 THEN(SNDPGMMSG ('NOT FOUND!'))

• Search from a specific position in the string. The %SCAN function is used multiple times to take a date value that contains a date separator and retrieve the month value into a numeric variable. The first %SCAN finds the position of the date separator between the day and month. The second %SCAN starts with the character that follows the first date separator and finds the position of the date separator between the month and year. The substring (%SST) built-in function is then used to convert the month value from character form to a decimal variable.

DCL VAR(&YYYYMMDD) TYPE(*CHAR) LEN(10) VALUE('2012/8/29')
DCL VAR(&DATSEP) TYPE(*CHAR) LEN(1) VALUE('/')
DCL VAR(&SPOS) TYPE(*UINT) LEN(2)
DCL VAR(&EPOS) TYPE(*UINT) LEN(2)
DCL VAR(&LEN) TYPE(*UINT) LEN(2)
DCL VAR(&MONTH) TYPE(*DEC) LEN(2)
CHGVAR VAR(&SPOS) VALUE(%SCAN(&DATSEP &YYYYMMDD))
CHGVAR VAR(&SPOS) VALUE(&SPOS + 1)
CHGVAR VAR(&EPOS) VALUE(%SCAN(&DATSEP &YYYYMMDD &SPOS))
CHGVAR VAR(&LEN) VALUE(&EPOS - &SPOS)
CHGVAR VAR(&MONTH) VALUE(%SST(&YYYYMMDD &SPOS &LEN))

• Search characters from the local data area (*LDA). The local data area (LDA) is scanned starting from byte 1 of the LDA for the string ’Escape’. If the string is found, the position of the string in the LDA is assigned to CL variable &POS. If the string ’Escape’ is not found in the whole LDA, a value of zero is assigned to variable &POS.

DCL VAR(&POS) TYPE(*UINT) LEN(2)
CHGVAR VAR(&POS) VALUE(%SCAN('Escape' *LDA))

 

%SIZE built-in function

The %SIZE built-in function returns the number of bytes occupied by the CL variable.

The format of the getting size built-in function is: %SIZE(variable-argument)

The following are examples of using the %SIZE built-in function:

• Get the size of numeric variables

DCL VAR(&NUM1) TYPE(*DEC)
DCL VAR(&NUM2) TYPE(*DEC) LEN(7 2)
DCL VAR(&NUM3) TYPE(*INT)
DCL VAR(&NUM4) TYPE(*UINT) LEN(8)
DCL VAR(&RTN) TYPE(*INT) LEN(2)
/* &RTN will have the value 8. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM1))
/* &RTN will have the value 4. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM2))
/* &RTN will have the value 4. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM3))
/* &RTN will have the value 8. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM4))

• Get the size of character variables

DCL VAR(&CHAR1) TYPE(*CHAR)
DCL VAR(&CHAR2) TYPE(*CHAR) LEN(20)
DCL VAR(&RTN) TYPE(*INT) LEN(2)
/* &RTN will have the value 32. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&CHAR1))
/* &RTN will have the value 20. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&CHAR2))
/* &RTN will have the value 52. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&CHAR1) + %SIZE(&CHAR2))

• Get the size of logical variables

DCL VAR(&LGL1) TYPE(*LGL)
DCL VAR(&RTN) TYPE(*INT) LEN(2)
/* &RTN will have the value 1. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&LGL1))

• Get the size of pointer variables

DCL VAR(&PTR1) TYPE(*PTR)
DCL VAR(&RTN) TYPE(*INT) LEN(2)
/* &RTN will have the value 16. */
CHGVAR VAR(&RTN) VALUE(%SIZE(&PTR1))

 

%SUBSTRING built-in function

The substring built-in function (%SUBSTRING or %SST) produces a character string that is a subset of an existing character string.

The format of the substring built-in function is shown in this example: %SUBSTRING(character-variable-name starting-position length)

The following examples are about the substring built-in function:

• If the first ttwo positions in the character variable &NAME are IN, the program INV210 is called. The entire value of &NAME is passed to INV210 and the value of &ERRCODE is unchanged. Otherwise, the value of &ERRCODE is set to 99.

DCL &NAME *CHAR VALUE(INVOICE)
DCL &ERRCODE *DEC (2 0)
IF (%SST(&NAME 1 2) *EQ 'IN') +
THEN(CALL INV210 &NAME)
ELSE CHGVAR &ERRCODE 99

• If the first two positions of &A match the first two positions of &B, the program CUS210 is called.

DCL &A *CHAR VALUE(ABC)
DCL &B *CHAR VALUE(DEF)
IF (%SST(&A 1 2) *EQ %SUBSTRING(&B 1 2)) +
CALL CUS210

 

%SWITCH built-in function

The switch built-in function (%SWITCH) compares one or more of eight switches with the eight switch settings already established for the job and returns a logical value of '0' or '1'.

The syntax of the %SWITCH built-in function is: %SWITCH(8-character-mask)

Each position in the mask can be specified as one of three values: 0, 1, or X.

0 The corresponding job switch is to be tested for a 0 (off).

1 The corresponding job switch is to be tested for a 1 (on).

X The corresponding job switch is not to be tested. 

If %SWITCH(0X111XX0) is specified, job switches 1 and 8 are tested for 0s; switches 3, 4, and 5 are tested for 1s; and switches 2, 6, and 7 are not tested. If each job switch contains the value (1 or 0 only) shown in the mask, the result of %SWITCH is true '1'.

%SWITCH with the IF command: On the If (IF) command, %SWITCH can be specified on the COND parameter as the logical expression to be tested. In the following example, 0X111XX0 is compared to the predetermined job switch setting:

IF COND(%SWITCH(0X111XX0)) THEN(GOTO C)

If job switch 1 contains 0, job switch 3 contains 1, job switch 4 contains 1, job switch 5 contains 1, and job switch 8 contains 0, the result is true and the procedure branches to the command having the label C. If one or more of the switches tested do not have the values indicated in the mask, the result is false, and the branch does not occur.

SBMJOB JOB(APP502) JOBD(PAYROLL) CMD(CALL APP502)
 SWS(11000000)
PGM /* CONTROL */
IF (%SWITCH(11XXXXXX)) CALLPRC PROCA
IF (%SWITCH(10XXXXXX)) CALLPRC PROCB
IF (%SWITCH(01XXXXXX)) CALLPRC PROCC
IF (%SWITCH(00XXXXXX)) CALLPRC PROCD
ENDPGM
PGM /* PROCA */
CALLPRC TRANS
IF (%SWITCH(1XXXXXXX)) CALLPRC CUS520
ELSE CALLPRC CUS521
ENDPGM

%SWITCH with the Change Variable command: On the Change Variable (CHGVAR) command, you can specify %SWITCH to change the value of a logical variable. The value of the logical variable is determined by the results of comparing your %SWITCH settings with the job switch settings.

If the result of the comparison is true, the logical variable is set to '1'. If the result is false, the variable is set to '0'.

For instance, if the job switch is set to 10000001 and this procedure is processed, then the variable &A has a value of '1':

PGM
DCL &A *LGL
CHGVAR VAR(&A) VALUE(%SWITCH(10000001))
.
.
.
ENDPGM

 

%TRIM built-in function

The trim built-in function (%TRIM) with one parameter produces a character string with any leading and trailing blanks removed.

The trim built-in function (%TRIM) with two parameters produces a character string with any leading and trailing characters that are in the characters to trim parameter removed.

The format of the trim built-in function is: %TRIM(character-variable-name [characters-to-trim])

The following examples are about the trim built-in function:

• Trim leading and trailing blank characters. The leading and trailing blanks are trimmed from the CL variables &FIRSTNAME and &LASTNAME and the resulting strings are concatenated with a single blank between the two values with the *BCAT operator. The concatenated string is then assigned to CL variable &NAME.

DCL VAR(&FIRSTNAME) TYPE(*CHAR) VALUE(' JOHN ')
DCL VAR(&LASTNAME) TYPE(*CHAR) VALUE(' SMITH ')
DCL VAR(&NAME) TYPE(*CHAR) LEN(10)
CHGVAR VAR(&NAME) VALUE(%TRIM(&FIRSTNAME) *BCAT %TRIM(&LASTNAME))

• Trim characters that are specified in a literal string. All asterisks and blanks are trimmed from the beginning and end of CL variable &NUM and the remaining characters (1.23) are assigned to CL variable &TRIMMED. The character variable &TRIMMED is converted to a numeric value and assigned to decimal variable &DEC.

DCL VAR(&NUM) TYPE(*CHAR) LEN(10) VALUE('* *1.23* *')
DCL VAR(&TRIMMED) TYPE(*CHAR) LEN(10)
DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)
CHGVAR &TRIMMED %TRIM(&NUM '* ')
CHGVAR VAR(&DEC) VALUE(&TRIMMED)

• Trim characters that are specified in a CL variable. Starting from the first character in variable &NAME, trim the character if it matches one of the characters in variable &TCHAR. Also, starting with the last character in variable &NAME, trim the character if it matches one of the characters in variable &TCHAR. The resulting substring of &NAME is compared to the literal string ’12345’ and a message is sent if the values are equal.

DCL VAR(&NAME) TYPE(*CHAR) LEN(10) VALUE('*+12345 ')
DCL VAR(&TCHAR) TYPE(*CHAR) LEN(3) VALUE('*+ ')
IF COND(%TRIM(&NAME &TCHAR) *EQ '12345') +
 THEN(SNDPGMMSG ('EQUAL!'))

 

%TRIML built-in function

The trim left built-in function (%TRIML) with one parameter produces a character string with any leading blanks removed. The trim left built-in function (%TRIML) with two parameters produces a character string with any leading characters that are in the characters to trim parameter removed.

The format of the trim left built-in function is: %TRIML(character-variable-name [characters-to-trim])

The following examples are about the trim left built-in function:

• Trim leading blank characters. The leading blanks are trimmed from the CL variable &NUMCHAR and the resulting string is converted to a numeric value and assigned to CL variable &NUMDEC.

DCL VAR(&NUMCHAR) TYPE(*CHAR) VALUE(' 00001')
DCL VAR(&NUMDEC) TYPE(*DEC) LEN(5)
CHGVAR VAR(&NUMDEC) VALUE(%TRIML(&NUMCHAR)) 

• Trim leading characters that are specified in a literal string. All dollar signs and blanks are trimmed from the beginning of CL variable &PRICE and the remaining characters (5.27) are assigned to CL variable &TRIMMED. The character variable &TRIMMED is converted to a numeric value and assigned to decimal variable &DEC.

DCL VAR(&PRICE) TYPE(*CHAR) VALUE(' $5.27')
DCL VAR(&TRIMMED) TYPE(*CHAR) LEN(5)
DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)
CHGVAR VAR(&TRIMMED) VALUE(%TRIML(&PRICE '$ '))
CHGVAR VAR(&DEC) VALUE(&TRIMMED)

• Trim leading characters that are specified in a CL variable. Starting from the first character in variable &NAME, trim the character if it matches one of the characters in variable &TCHAR. The resulting substring of &NAME is compared to the literal string ’12345’ and a message is sent if the values are equal.

DCL VAR(&NAME) TYPE(*CHAR) LEN(10) VALUE(' +12345')
DCL VAR(&TCHAR) TYPE(*CHAR) LEN(2) VALUE('+ ')
IF COND(%TRIML(&NAME &TCHAR) *EQ '12345') +
 THEN(SNDPGMMSG ('EQUAL!'))

 

%TRIMR built-in function

The trim right built-in function (%TRIMR) with one parameter produces a character string with any trailing blanks removed.

The trim right built-in function (%TRIMR) with two parameters produces a character string with any trailing characters that are in the characters to trim parameter removed.

The format of the trim right built-in function is: %TRIMR(character-variable-name [characters-to-trim])

The following examples are about the trim right built-in function:

• Trim trailing blank characters. The trailing blank characters are trimmed from the CL variables &FIRSTNAME and &LASTNAME and the resulting strings are concatenated with the *CAT operator. The concatenated string is then assigned to CL variable &NAME.

DCL VAR(&FIRSTNAME) TYPE(*CHAR) LEN(10) VALUE('JOHN ')
DCL VAR(&LASTNAME) TYPE(*CHAR) LEN(10) VALUE('SMITH ')
DCL VAR(&NAME) TYPE(*CHAR) LEN(10)
CHGVAR VAR(&NAME) VALUE(%TRIM(&FIRSTNAME) *CAT %TRIM(&LASTNAME))

• Trim trailing characters that are specified in a literal string. All trailing zero and plus sign characters are trimmed from CL variable &PRICE and the remaining characters (5.27) are assigned to CL variable &TRIMMED. The character variable &TRIMMED is converted to a numeric value and assigned to decimal variable &DEC.

DCL VAR(&PRICE) TYPE(*CHAR) LEN(10) VALUE('5.2700000+')
DCL VAR(&TRIMMED) TYPE(*CHAR) LEN(5)
DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)
CHGVAR VAR(&TRIMMED) VALUE(%TRIMR(&PRICE '0+'))
CHGVAR VAR(&DEC) VALUE(&TRIMMED)

• Trim trailing characters that are specified in a CL variable. Starting from the last character in variable &NAME, trim the character if it matches one of the characters in variable &TCHAR. The resulting substring of &NAME is compared to the literal string ’12345’ and a message is sent if the values are equal.

DCL VAR(&NAME) TYPE(*CHAR) LEN(10) VALUE('12345*** ')
DCL VAR(&TCHAR) TYPE(*CHAR) LEN(2) VALUE('* ')
IF COND(%TRIMR(&NAME &TCHAR) *EQ '12345') +
 THEN(SNDPGMMSG ('EQUAL!'))

 

%UINT built-in function

%UINT converts character, logical, decimal, or integer data to unsigned integer format.

The converted value can be assigned to a CL variable, passed as a numeric constant to another program or procedure, or specified as a value for a command parameter of a CL command run from compiled CL.

The format of the convert to unsigned integer data built-in function is:

%UINT(convert-argument)

The following are examples of using the %UINT built-in function:

• Convert character variable

DCL VAR(&POINTS) TYPE(*CHAR) LEN(10) VALUE('+123.45')
DCL VAR(&ANSWER) TYPE(*UINT)
/* &ANSWER will have the value 223 */
CHGVAR VAR(&ANSWER) VALUE(100 + %UINT(&POINTS))

• Convert logical variable

DCL VAR(&ANSWER1) TYPE(*LGL) VALUE('1')
DCL VAR(&ANSWER2) TYPE(*LGL) VALUE('1')
DCL VAR(&NUM) TYPE(*UINT)
/* &NUM will have the value 2. */
CHGVAR VAR(&NUM) VALUE(%UINT(&ANSWER1) + %UINT(&ANSWER2))
SNDPGMMSG MSG('The number of YES answers is' *BCAT %CHAR(&NUM))

• Convert packed decimal variable

DCL VAR(&POINTS1) TYPE(*DEC) LEN(5 2) VALUE(100.23)
DCL VAR(&POINTS2) TYPE(*DEC) LEN(5 2) VALUE(100.45)
IF (%UINT(&POINTS1) *EQ %UINT(&POINTS2)) +
THEN(SNDPGMMSG ('The scores are the same!'))

• Convert integer variable

DCL VAR(&P1) TYPE(*UINT) LEN(2)
DCL VAR(&P2) TYPE(*INT) LEN(2) VALUE(1)
CHGVAR VAR(&P1) VALUE(%UINT(&P2))

• Decimal digits will be truncated without rounding

DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('+123.9')
DCL VAR(&ANSWER) TYPE(*UINT)
/* &ANSWER will have the value 123 */
CHGVAR VAR(&ANSWER) VALUE(%UINT(&STRING))

 

%UPPER built-in function

The %UPPER built-in function returns a character string that is the same length as the argument specified with each lowercase letter replaced by the corresponding uppercase letter.

The format of the convert to uppercase built-in function is: %UPPER(input-string [CCSID])

The following are examples of using the %UPPER built-in function:
• Convert to uppercase

DCL VAR(&STR) TYPE(*CHAR) LEN(12) VALUE('Hello World!')
/* 'HELLO WORLD!' is to be sent */
SNDPGMMSG (%UPPER(&STR))

• Convert to uppercase based on CCSID

/* define &STR as 'Hello World!' in CCSID 819 (ISO/ANSI Multilingual) */
DCL VAR(&STR) TYPE(*CHAR) LEN(12) +
 VALUE(X'48656C6C6F20576F726C6421')
/* &STR will have the value x'48454C4C4F20574F524C4421' ('HELLO WORLD!') */
CHGVAR VAR(&STR) VALUE(%UPPER(&STR 819))

 

Post Comments