INSPECT
A COBOL statement that counts, replaces or converts characters inside a field.
Also written INSPECT TALLYING, INSPECT REPLACING, INSPECT CONVERTING
INSPECT looks through a field character by character. It has three main forms.
TALLYING counts. INSPECT WS-LINE TALLYING WS-COUNT FOR ALL ',' adds the number of commas in the line to WS-COUNT. You can count all occurrences, only leading ones, or the characters before or after a given value.
REPLACING changes characters in place. INSPECT WS-NAME REPLACING ALL LOW-VALUES BY SPACES turns unseen low values into spaces, a common clean-up for data that arrived from another system. The replacement must be the same length as the text it replaces.
CONVERTING changes a set of characters to another set in one statement. INSPECT WS-CODE CONVERTING 'abcdefghijklmnopqrstuvwxyz' TO 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' changes lower case to upper case. Newer COBOL also offers intrinsic functions, such as FUNCTION UPPER-CASE, for the same result.
INSPECT is useful for checking and cleaning input before it is used. Be careful with changes that alter meaning, such as replacing spaces with zeros in a numeric field: it avoids an abend, but it may hide a real problem in the data.
Where can I learn this hands-on?
Right here, at ZedBank. You take a junior job at a simulated bank and learn each part of the work by doing it, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- STRING and UNSTRINGTwo COBOL statements: STRING joins several fields into one, and UNSTRING splits one field into several.
- MOVEThe COBOL statement that copies a value into a field, converting it to fit the receiving field's format.
- PICTURE clauseThe COBOL declaration that says what kind of data a field holds and how long it is, written as PIC.
- PROCEDURE DIVISIONThe part of a COBOL program that contains the actual logic, the statements that run.
- Zoned decimalThe default way COBOL stores numbers: one digit per byte, readable as text, with the sign hidden in the last byte.