%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 %CHECK built-in function can be used anywhere that CL supports an arithmetic expression. %CHECK can be used alone or as part of a more complex arithmetic expression. For example, %CHECK can be used to compare to a numeric CL variable in the COND parameter of an IF or WHEN command. %CHECK can also be used to set the value of a CL command parameter, if the associated command object defines the parameter with EXPR(*YES) and TYPE of *DEC, *INT2, *INT4, *UINT2, or *UINT4.

The format of the check built-in function is:
%CHECK(comparator-string base-string [starting-position])

The comparator string must be either a CL character variable or a character literal. The base string can be a CL character variable or *LDA. When *LDA is specified, the check function is performed on the contents of the local data area for the job. The starting position is optional and defaults to 1. Checking begins at the starting position (start) and continues to the right until a character that is not contained in the comparator string is found. The result is always relative to the first byte in the source string, even if the starting position is specified. The starting position, if specified, must be either a CL integer variable or a CL decimal variable with zero decimal positions or a numeric literal with zero decimal positions. The starting position cannot be zero or negative. If the starting position is greater than the length of the entire base variable or the local data area, an error occurs. The length of the local data area is 1024.

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))