TALLYING is a clause used with INSPECT, STRING, and UNSTRING statements to count occurrences of characters, strings, or patterns in data. TALLYING counts how many times specified characters appear and stores the count in a numeric variable. It's useful for data validation, parsing, and analyzing string content.
123456789101112131415161718192021222324WORKING-STORAGE SECTION. 01 INPUT-FIELD PIC X(50). 01 CHAR-COUNT PIC 9(4) VALUE 0. 01 SPACE-COUNT PIC 9(4) VALUE 0. PROCEDURE DIVISION. COUNT-CHARACTERS. MOVE 'HELLO WORLD' TO INPUT-FIELD *> Count all characters INSPECT INPUT-FIELD TALLYING CHAR-COUNT FOR CHARACTERS *> CHAR-COUNT = 11 *> Count specific character MOVE 0 TO SPACE-COUNT INSPECT INPUT-FIELD TALLYING SPACE-COUNT FOR ALL ' ' *> SPACE-COUNT = 1 DISPLAY 'Total characters: ' CHAR-COUNT DISPLAY 'Spaces: ' SPACE-COUNT STOP RUN.
123456789101112131415161718WORKING-STORAGE SECTION. 01 TEXT-FIELD PIC X(100). 01 A-COUNT PIC 9(4) VALUE 0. 01 E-COUNT PIC 9(4) VALUE 0. PROCEDURE DIVISION. COUNT-MULTIPLE. MOVE 'COBOL PROGRAMMING' TO TEXT-FIELD *> Count multiple characters INSPECT TEXT-FIELD TALLYING A-COUNT FOR ALL 'A' E-COUNT FOR ALL 'E' DISPLAY 'A count: ' A-COUNT DISPLAY 'E count: ' E-COUNT STOP RUN.
1. What does TALLYING do?
2. What must you do before using TALLYING?
3. Which statement uses TALLYING?