TExcelTAD
Home › COBOL › COBOL INSPECT & Reference Modification

COBOL INSPECT & Reference Modification

INSPECT counts and replaces characters inside a field. Reference modification picks a substring by position. Together they cover most of the text processing that business programs need: cleaning input, masking account numbers, and extracting parts of codes.

INSPECT TALLYING: counting

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INSPECT-COUNT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-TEXT            PIC X(40)
           VALUE "the quick brown fox jumps over the dog".
       01  WS-SPACES          PIC 99      VALUE 0.
       01  WS-THE             PIC 99      VALUE 0.
       01  WS-O               PIC 99      VALUE 0.
       01  WS-LEADING         PIC 99      VALUE 0.
       01  WS-WORDS           PIC 99.
       01  WS-NUMBER          PIC X(8)    VALUE "00042500".
       PROCEDURE DIVISION.
           INSPECT WS-TEXT TALLYING WS-THE FOR ALL "the"
                                    WS-O FOR ALL "o"
           INSPECT FUNCTION TRIM(WS-TEXT)
               TALLYING WS-SPACES FOR ALL SPACE
           INSPECT WS-NUMBER TALLYING WS-LEADING FOR LEADING "0"
           DISPLAY "'the' appears " WS-THE " times"
           DISPLAY "'o' appears   " WS-O " times"
           COMPUTE WS-WORDS = WS-SPACES + 1
           DISPLAY "Words:        " WS-WORDS
           DISPLAY "Leading zeros in " WS-NUMBER ": " WS-LEADING
           STOP RUN.
'the' appears 02 times
'o' appears   04 times
Words:        08
Leading zeros in 00042500: 03

Note: TALLYING adds to the counter; it does not reset it. Always set counters to zero first.

FOR ALL x counts every occurrence, FOR LEADING x counts only those at the start, and FOR CHARACTERS counts every character. BEFORE INITIAL x / AFTER INITIAL x limit the range.

INSPECT REPLACING and CONVERTING

REPLACING swaps characters or strings of the same length. CONVERTING maps each character of one list to the character at the same position in another, like tr in Unix:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INSPECT-REPLACE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-PHONE           PIC X(14)   VALUE "(555) 123-4567".
       01  WS-AMOUNT          PIC X(10)   VALUE "  1234.50 ".
       01  WS-NAME            PIC X(12)   VALUE "jean-luc".
       01  WS-DATE            PIC X(10)   VALUE "2026.03.15".
       PROCEDURE DIVISION.
           INSPECT WS-PHONE REPLACING ALL "(" BY SPACE
                                      ALL ")" BY SPACE
                                      ALL "-" BY SPACE
           INSPECT WS-AMOUNT REPLACING LEADING SPACE BY "*"
           INSPECT WS-NAME CONVERTING "abcdefghijklmnopqrstuvwxyz"
                                   TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           INSPECT WS-DATE REPLACING ALL "." BY "-"
           DISPLAY "[" WS-PHONE "]"
           DISPLAY "[" WS-AMOUNT "]"
           DISPLAY "[" WS-NAME "]"
           DISPLAY "[" WS-DATE "]"
           STOP RUN.
[ 555  123 4567]
[**1234.50 ]
[JEAN-LUC    ]
[2026-03-15]

Reference modification: substrings

item (start : length) refers to length characters starting at position start (1-based). Leave out the length to take everything up to the end. Reference modification works on the left side of a MOVE too, so you can overwrite part of a field:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. REF-MOD.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-IBAN            PIC X(22)
           VALUE "DE89370400440532013000".
       01  WS-DATE            PIC X(8)    VALUE "20260315".
       01  WS-MASKED          PIC X(22).
       01  WS-START           PIC 99      VALUE 5.
       PROCEDURE DIVISION.
           DISPLAY "Country:   " WS-IBAN (1:2)
           DISPLAY "Check:     " WS-IBAN (3:2)
           DISPLAY "Bank code: " WS-IBAN (WS-START:8)
           DISPLAY "Rest:      " WS-IBAN (13:)
           DISPLAY "Date:      " WS-DATE (7:2) "/" WS-DATE (5:2) "/"
                                 WS-DATE (1:4)
           MOVE WS-IBAN TO WS-MASKED
           MOVE ALL "*" TO WS-MASKED (5:14)
           DISPLAY "Masked:    " WS-MASKED
           STOP RUN.
Country:   DE
Check:     89
Bank code: 37040044
Rest:      0532013000
Date:      15/03/2026
Masked:    DE89**************3000

The start and length can be data items or expressions, which makes reference modification the tool for loops over the characters of a field.

Walking through characters

This program reverses a word and checks whether it is a palindrome, using a loop over positions:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PALINDROME.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-WORD            PIC X(20)   VALUE "Racecar".
       01  WS-UPPER           PIC X(20).
       01  WS-REVERSED        PIC X(20)   VALUE SPACES.
       01  WS-LEN             PIC 99.
       01  WS-I               PIC 99.
       PROCEDURE DIVISION.
           MOVE FUNCTION UPPER-CASE(WS-WORD) TO WS-UPPER
           MOVE FUNCTION LENGTH(FUNCTION TRIM(WS-UPPER)) TO WS-LEN
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > WS-LEN
               MOVE WS-UPPER (WS-I:1)
                 TO WS-REVERSED (WS-LEN - WS-I + 1:1)
           END-PERFORM
           DISPLAY "Word:     " WS-UPPER (1:WS-LEN)
           DISPLAY "Reversed: " WS-REVERSED (1:WS-LEN)
           IF WS-REVERSED = WS-UPPER
               DISPLAY "It is a palindrome"
           ELSE
               DISPLAY "Not a palindrome"
           END-IF
           STOP RUN.
Word:     RACECAR
Reversed: RACECAR
It is a palindrome

GnuCOBOL also has FUNCTION REVERSE, but writing the loop once shows how reference modification works.

Warning: Reference modification outside the field (WS-DATE (7:5) on an 8-byte field) is not checked by default and reads neighbouring storage. cobc -debug turns on run-time checks.

Test yourself

What is WS-CODE (3:2) if WS-CODE contains "ABCDEF"?

  1. "BC"
  2. "CD"
  3. "CDE"
  4. "DE"

What does INSPECT X TALLYING N FOR ALL "A" do to N?

  1. Sets N to the count
  2. Adds the count to N
  3. Subtracts the count
  4. Nothing, it only replaces

Which INSPECT phrase maps each character in one list to the matching character in another?

  1. REPLACING ALL
  2. TALLYING
  3. CONVERTING
  4. TRANSLATING