%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 %CHECKR built-in function can be used anywhere that CL supports an arithmetic expression. %CHECKR can be used alone or as part of a more complex arithmetic expression. For example, %CHECKR can be used to compare to a numeric CL variable in the COND parameter of an IF or WHEN command. %CHECKR 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.
%CHECKR(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 reverse check function is performed on the contents of the local data area for the job. The starting position is optional and defaults to the last byte of the base string. Checking begins at the starting position (start) and continues to the left 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 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))