Monday, September 9, 2013

Different forms of INSPECT clause in cobol

Below are the different INSPECT with REPLACE clause and their usage in cobol.

INSPECT WS-TEXT      REPLACING ALL LOW-VALUES BY SPACE
INSPECT WS-TEXT      REPLACING ALL ' ' BY '0'
INSPECT WS-TEXT      REPLACING ALL LOW-VALUES BY SPACES
INSPECT WS-TEXT      REPLACING FIRST '*' BY '%'
INSPECT WS-TEXT      REPLACING ALL '~' BY WS-INSPECT-VAL
INSPECT WS-TEXT      REPLACING ALL WS-INSPECT-VAL  BY '('
INSPECT WS-TEXT      REPLACING ALL  '('   BY    ')'     AFTER INITIAL '('

**
01 VAR-STRING PIC X(20) VALUE 'THIS IS COBOL'.
01 VAR-COUNT PIC 9(2).

INSPECT VAR-STRING TALLYING VAR-COUNT FOR ALL 'S'
O/P : VAR-COUNT = 2
**
VAR-STRING = "THIS IS COBOL"
INSPECT VAR-STRING TALLYING VAR-COUNG FOR ALL "S" AFTER INITIAL SPACE.

O/P : VAR-COUNT = 1 (Since first 'S' wont be counted . All the 'S' after the first space will be counted.)
**
VAR-STRING = "MY COBOL PROGRAMMING COURSE"
INSPECT VAR-STRING TALLYING VAR-COUNT FOR ALL LEADING 'M'

O/P : VAR-COUNT = 1 (ONLY THE FIRST 'M' WILL BE TAKEN INTO ACCOUNT)
**
01 WS-INPUT3='PTEST'
INSPECT WS-INPUT3 CONVERTING 'PTHASI' TO '3090RI'.
O/P:  30ES0
The exact number of characters need to be compared and mapped. Also duplicate is not
allowed in the parameters.
ex: INSPECT WS-INPUT3 CONVERTING 'PTPTSI' TO '3090RI' will result in error
because 'P' and 'T' is duplicated.
**