CL Programming

Using the %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 and can only be used within a CL procedure. In a CHGVAR command, the %SST function can be specified in place of the variable (VAR parameter) to be changed or the value (VALUE parameter) to which the variable is to be changed. In an IF command, the %SST function can be specified in the expression.

The format of the substring built-in function is:

%SUBSTRING(character-variable-name starting-position length)

or

%SST(character-variable-name starting-position length)

You can code *LDA in place of the character variable name to indicate that the substring function is performed on the contents of the local data area.

The substring function produces a substring from the contents of the specified CL character variable or the local data area. The substring begins at the specified starting position (which can be a variable name) and continues for the length specified (which can also be a variable name). Neither the starting position nor the length can be 0 or negative. If the sum of the starting position and the length of the substring are greater than the length of the entire variable or the local data area, an error occurs. The length of the local data area is 1024.

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

The following procedure uses the substring built-in function to find the first sentence in a 50-character field &INPUT and to place any remaining text in a field &REMAINDER It assumes that a sentence must have at least 2 characters, and no embedded periods.

        PGM (&INPUT &REMAINDER)   /*  SEARCH  */
        DCL &INPUT *CHAR LEN(50)
        DCL &REMAINDER *CHAR LEN(50)
        DCL &X *DEC LEN(2 0) VALUE(03)
        DCL &L *DEC LEN(2 0)   /*  REMAINING LENGTH  */
SCAN:   IF (%SST(&INPUT &X 1) *EQ '.') THEN(DO)
            CHGVAR VAR(&L) VALUE(50-&X)
            CHGVAR VAR(&X) VALUE(&X+1)
            CHGVAR VAR(&REMAINDER) VALUE(%SST(&INPUT &X &L))
            RETURN
            ENDDO
        IF (&X *EQ 49) THEN(RETURN)
        CHGVAR &X (&X+1)
        GOTO SCAN
        ENDPGM

The procedure starts by checking the third position for a period. Note that the substring function checks &INPUT from position 3 to a length of 1, which is position 3 only (length cannot be zero). If position 3 is a period, the remaining length of &INPUT is calculated. The value of &X is advanced to the beginning of the remainder, and the remaining portion of &INPUT is moved to &REMAINDER.

If position 3 is not a period, the procedure checks to see if it is at position 49. If so, it assumes that position 50 is a period and returns. If it is not at position 49, the procedure advances &X to position 4 and repeats the process.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]