INSPECT TALLYING
INSPECT reads a field without changing it, and TALLYING is the form that counts. It is the COBOL way to ask how many spaces are in a line, how many times a word appears, or how long the first word is, and it answers in one statement.
Counting one character
One sentence with five spaces in it, and four counters that each answer a different question about the same bytes:
IDENTIFICATION DIVISION.
PROGRAM-ID. COUNT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT".
01 SPACES-C PIC 9(2) VALUE 0.
01 THE-C PIC 9(2) VALUE 0.
01 E-C PIC 9(2) VALUE 0.
01 BEFORE-C PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
INSPECT LINE-F TALLYING SPACES-C FOR ALL SPACES.
INSPECT LINE-F TALLYING THE-C FOR ALL "THE".
INSPECT LINE-F TALLYING E-C FOR ALL "E".
INSPECT LINE-F TALLYING BEFORE-C FOR ALL "A" BEFORE INITIAL "ON".
DISPLAY "[" LINE-F "]".
DISPLAY "spaces " SPACES-C.
DISPLAY "THE " THE-C.
DISPLAY "E " E-C.
DISPLAY "A before ON " BEFORE-C.
STOP RUN.
cobc -x -free -Wall -o count count.cob
./count
[THE CAT SAT ON THE MAT]
spaces 05
THE 02
E 02
A before ON 02
Five spaces is one more than the number of words minus one, and that is how a program finds the number of words in a line. The letter E appears in THE twice, and the word THE appears twice; the two counts happen to be equal here, which is a coincidence of this sentence and a good reason to check a count against a hand count on a real line.
A literal longer than one character
FOR ALL "THE" counts non-overlapping occurrences of the three characters, not the three letters separately. A literal of several characters is compared at every position of the field, and after a match the scan continues after it, so "AAA" in "AAAAA" counts once, not three times.
The counter has to be wide enough for the worst case, and the worst case is the length of the field: a PIC 99 counter on a 200-byte line overflows silently at 100, the same way any other field does. The counter starts at whatever it holds, so a program that tallies twice into the same field adds the counts and has to MOVE 0 between them.
BEFORE and AFTER
A scan that covers the whole field is often not what a program wants, and the two phrases limit it. BEFORE INITIAL "ON" counts only the characters before the first ON, which is why the fourth counter says 2 and not 3: the third A is in MAT, after the delimiter.
INITIAL means the first occurrence of the delimiter, and a delimiter of several characters is matched as a whole. AFTER INITIAL starts the scan after it, which is how a program counts the rest of a line once a header has ended.
CHARACTERS
The word CHARACTERS counts positions instead of a character, so it measures a length. With BEFORE INITIAL SPACE it is the length of the first word, and with AFTER INITIAL "T" it is what is left after the first letter T:
IDENTIFICATION DIVISION.
PROGRAM-ID. CHARS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT".
01 WORD-C PIC 9(2) VALUE 0.
01 TAIL-C PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
INSPECT LINE-F TALLYING WORD-C FOR CHARACTERS BEFORE INITIAL SPACE.
INSPECT LINE-F TALLYING TAIL-C FOR CHARACTERS AFTER INITIAL "T".
DISPLAY "first word " WORD-C.
DISPLAY "after T " TAIL-C.
STOP RUN.
cobc -x -free -Wall -o chars chars.cob
./chars
first word 03
after T 21
Three is the length of THE, and twenty-one is the rest of the twenty-two characters after the first one. Neither statement needed a loop or a substring: the scan does the walking and the counter does the arithmetic, which is why a COBOL program that parses a line looks shorter than the same program in C.
Exercises
- Count the words in the sentence by counting spaces and adding one. What does the program print, and is the last word followed by a space?
- Count the occurrences of "AT" in the line. How many are there, and where does the scan go after a match?
- Count the characters before the first "A" and after the last one. Which of the two phrases gives the position of the last letter?
- Give the line a trailing full stop and count the characters before it. What does the count prove about the field?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does INSPECT TALLYING do to the field it reads?
Answer
Nothing: it counts occurrences and leaves the bytes alone
TALLYING is the read-only form of INSPECT; the REPLACING form is what changes the field.
-
What does FOR ALL "THE" count?
Answer
Non-overlapping occurrences of the three characters together
The literal is matched as a whole at every position, and the scan continues after each match.
-
What does BEFORE INITIAL "ON" change about a tally?
Answer
Only the part of the field before the first ON is scanned
INITIAL marks the first occurrence of the delimiter, and BEFORE stops the scan there.
-
What does FOR CHARACTERS count?
Answer
Positions, so with a delimiter it measures a length
CHARACTERS counts every position the scan passes, which is a length when a delimiter bounds it.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". 01 SPACES-C PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT LINE-F TALLYING SPACES-C FOR ALL SPACES. DISPLAY "[" SPACES-C "]". STOP RUN.Answer
[05]
The sentence has five spaces, one between each pair of words, and the counter prints with two digits.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". 01 THE-C PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT LINE-F TALLYING THE-C FOR ALL "THE". DISPLAY "[" THE-C "]". STOP RUN.Answer
[02]
The literal THE appears at the start and again near the end of the sentence.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". 01 BEFORE-C PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT LINE-F TALLYING BEFORE-C FOR ALL "A" BEFORE INITIAL "ON". DISPLAY "[" BEFORE-C "]". STOP RUN.Answer
[02]
The three As are in CAT, SAT and MAT; the last one is after the ON, so only the first two are in the scan.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". 01 WORD-C PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT LINE-F TALLYING WORD-C FOR CHARACTERS BEFORE INITIAL SPACE. DISPLAY "[" WORD-C "]". STOP RUN.Answer
[03]
The scan stops at the first space, which comes after the three letters of THE.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 LINE-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". 01 TAIL-C PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT LINE-F TALLYING TAIL-C FOR CHARACTERS AFTER INITIAL "T". DISPLAY "[" TAIL-C "]". STOP RUN.Answer
[21]
The scan starts after the first T, so it counts the other twenty-one positions of the field.
-
A counter of two digits tallies the spaces of a 200-byte line. What is the risk?
Answer
The count wraps at a hundred, with no message
The counter is an ordinary numeric field, so a count that does not fit truncates the same way an addition would.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev