Counting and replacing with INSPECT
INSPECT examines a field character by character. It can count
characters (TALLYING), replace them (REPLACING), or translate
them one-for-one (CONVERTING). It changes the field in place.
TALLYING
IDENTIFICATION DIVISION.
PROGRAM-ID. INSTALLY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TEXT PIC X(20) VALUE " 12,500,000.00".
01 WS-COMMAS PIC 9(2).
01 WS-BLANKS PIC 9(2).
01 WS-BEFORE PIC 9(2).
PROCEDURE DIVISION.
MOVE 0 TO WS-COMMAS WS-BLANKS WS-BEFORE
INSPECT WS-TEXT TALLYING WS-COMMAS FOR ALL ","
INSPECT WS-TEXT TALLYING WS-BLANKS FOR LEADING SPACE
INSPECT WS-TEXT TALLYING WS-BEFORE
FOR CHARACTERS BEFORE INITIAL "."
DISPLAY "COMMAS: " WS-COMMAS
DISPLAY "LEADING SPACES: " WS-BLANKS
DISPLAY "CHARACTERS BEFORE POINT: " WS-BEFORE
STOP RUN.
Output:
COMMAS: 02
LEADING SPACES: 02
CHARACTERS BEFORE POINT: 12
FOR ALL "x"counts every occurrence.FOR LEADING "x"counts occurrences at the start, stopping at the first character that does not match.FOR CHARACTERScounts every character. AddBEFORE INITIAL "x"orAFTER INITIAL "x"to limit INSPECT to part of the field; this works with the other forms too.
Like UNSTRING's tally, the counter is added to, so zero it first.
REPLACING
IDENTIFICATION DIVISION.
PROGRAM-ID. INSREPL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ACCT PIC X(8) VALUE " 45120".
01 WS-PHONE PIC X(12) VALUE "021-555-0199".
01 WS-NOTE PIC X(20) VALUE "Late fee, waived".
PROCEDURE DIVISION.
INSPECT WS-ACCT REPLACING LEADING SPACE BY "0"
DISPLAY WS-ACCT
INSPECT WS-PHONE REPLACING ALL "-" BY SPACE
DISPLAY WS-PHONE
INSPECT WS-PHONE REPLACING FIRST "5" BY "X"
DISPLAY WS-PHONE
INSPECT WS-NOTE CONVERTING "abcdefghijklmnopqrstuvwxyz,"
TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ;"
DISPLAY WS-NOTE
STOP RUN.
Output:
00045120
021 555 0199
021 X55 0199
LATE FEE; WAIVED
REPLACING LEADING SPACE BY "0"zero-fills a right-aligned number, a very common clean-up before moving text into a numeric field.REPLACING ALLchanges every occurrence;FIRSTchanges only the first.- The replacement must be the same length as what it replaces. INSPECT never makes a field longer or shorter. To remove characters, replace them with spaces and close the gaps another way.
CONVERTING
CONVERTING "abc" TO "xyz" translates each character in the first string
into the character at the same position in the second: every a becomes
x, every b becomes y, and so on. The last statement above upper-cases
the note and turns commas into semicolons in a single pass. Both strings
must be the same length.
You can also count and replace in one statement,
INSPECT f TALLYING n FOR ALL "," REPLACING ALL "," BY ";". The tally is
taken before any replacing happens.
On the job
INSPECT is the classic data-cleansing tool, and much older than the intrinsic functions you will meet next. You will find it zero-filling keys, stripping characters that would break a downstream file format, translating lowercase to uppercase, and counting delimiters to check a record has the right number of fields before it is unstrung.
Your task
Call-centre notes are being added to a comma-separated extract, so they must not contain commas. Read two lines from input:
- A free-text note (
WS-NOTE,PIC X(50)), e.g.late fee, waived, call back - An account number (
WS-ACCT,PIC X(8)), right-aligned with leading spaces, e.g.45120
Then:
- count the commas in the note,
- convert the note to upper case and every comma to a semicolon,
- zero-fill the account number.
Display:
ACCOUNT: 00045120
COMMAS: 02
NOTE: LATE FEE; WAIVED; CALL BACK
Use INSPECT for all three jobs.