INSPECT Statement
The INSPECT statement provides TALLYING, REPLACING, and CONVERTING functions for counting and modifying elements in character strings within a program.
Format 1
inspect identifier-1
[ tallying tallying-phrase ]
[ replacing replacing-phrase ]
[ after-before-phrase ]
Format 2
inspect identifier-1
[converting converting-phrase ]
[after-before-phrase ]
Syntax:
identifier-n is a data element, literal, or data returned from a function call.
Tallying phrase format
tallying numeric-data-1 for [ CHARACTERS ]
[ { ALL }
{ LEADING } identifier-2 ]
{ FIRST }
{ TRAILING }
Syntax:
numeric-data-nis a numeric data item.identifier-nis a data element, literal, or data returned from a function call.
General Rules:
- The
TALLYINGclause is designed to count the number ofCHARACTERS, or the iterations of a character string found inidentifier-1, or in a subset of the characters inidentifier-1as defined by the after before clause. - The
CHARACTERSphrase causes a simple count of the number of characters in the named identifier to be tallied, and stored innumeric-data-1. - The
ALLphrase causes a count of the number of iterations ofidentifier-2in the named identifier to be tallied, and stored innumeric-data-1. - The
LEADINGphrase causes a count of the number of contiguous iterations ofidentifier-2, starting at the left most position in the named identifier. - The
FIRSTphrase causesnumeric-data-1to be incremented if/when the first iteration ofidentifier-2is located in the named identifier. - The
TRAILINGphrase causes a count of the number of contiguous iterations ofidentifier-2, starting at the right-most position in the named identifier, and parsing from right to left.
Replacing phrase format:
replacing [ CHARACTERS ]
[ { ALL }
{ LEADING } identifier-3 ] BY identifier-4
{ FIRST }
{ TRAILING }
Syntax:
identifier-n is a data element, literal, or data returned from a function call.
General Rules:
- The
REPLACINGclause is designed to identify a string of characters inidentifier-1or in a subset of the characters inidentifier-1as defined by the after before clause, and replace them with an alternative string of characters of the same size. - The
CHARACTERSphrase causes every character inidentifier-1to be replaced byidentifier-4. When using theCHARACTERSphrase,identifier-3is an alphanumeric data item that is a single character. - The
ALLphrase causes all instances ofidentifier-3insideidentifier-1to be replaced byidentifier-4. - The
LEADINGphrase causes all contiguous iterations ofidentifier-3insideidentifier-1, starting at the left most position inidentifier-1to be replaced byidentifier-4. - The
FIRSTphrase causes the first iteration ofidentifier-3to be replaced byidentifier-4. - The
TRAILINGphrase causes all contiguous iterations ofidentifier-3insideidentifier-1, starting at the right most position in the named identifier, and parsing from right to left, to be replaced withidentifier-4 - Note that when using the
ALL,LEADING,FIRST, andTRAILINGphrases,identifier-3andidentifier-4must be strings of the same length.after-beforephrase format:
[ { BEFORE } INITIAL identifier-5 ]
{ AFTER }
Syntax:
identifier-n is a data element, literal, or data returned from a function call.
General Rules:
- The optional
AFTER-BEFOREphrase specifies the end-point or beginning-point in the named identifier that will be parsed in theINSPECTstatement. - The
BEFOREphrase causes theINSPECT/TALLYING/REPLACINGstatement to locate a parsing end point in the named identifier. Parsing begins at the first byte of the named identifier, and ends when the string named inidentifier-5is located. - The
AFTERphrase causes theINSPECT/TALLYING/REPLACINGstatement to locate a parsing beginning point in the named identifier. The named identifier is scanned from right to left. If anidentifier-5is located, it marks the parsing beginning-point, and parsing continues to the end of the identifier.CONVERTINGphrase format:
[converting identifier-2 TO identifier-3]
[ after-before-phrase ]
Syntax:
identifier-n is a data element, literal, or data returned from a function call.
General Rules:
- The
CONVERTINGphrase dictates a conversion of characters withinidentifier-1according to rules established by the ordinal positions of characters inidentifier-2andidentifier-3. To clarify with an example, the statement: INSPECT identifier-1 CONVERTING “AB ” TO “CD”causes every instance of“A”inidentifier-1(the first character inidentifier-2), to be converted to“C”(the first character inidentifier-3), and causes every instance of“B”inidentifier-1(the second character inidentifier-2) to be replaced by“D”(the second character inidentifier-3).- To further clarify,
INSPECT CONVERTINGis commonly used to convert lower-case characters to upper-case, as follows:INSPECT identifier-1 CONVERTING “abcdefghijklmnopqrstuvwxyz” to “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.
The effect of this statement is to convert every lower case letter inidentifier-1to its corresponding upper-case letter.after-beforephrase format:
[ { BEFORE } INITIAL identifier-5 ]
{ AFTER }
Syntax:
identifier-n is a data element, literal, or data returned from a function call.
General Rules:
- The optional
AFTER-BEFOREphrase specifies the end point or beginning point in the named identifier that will be parsed in theINSPECTstatement. - The
BEFOREphrase causes theINSPECT/CONVERTINGstatement to locate a parsing end-point in the named identifier. Parsing begins at the first byte of the named identifier, and ends when the string named inidentifier-6is located . - The
AFTERphrase cases theINSPECT/CONVERTINGstatement to locate a parsing beginning-point in the named identifier. The named identifier is scanned from right to left. If anidentifier-6is located, it marks the parsing beginning-point, and parsing continues to the end of the identifier.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. INSPECT-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 TALLY-CTR1 PIC 99 VALUE 0.
77 STRING-1 PIC X(10) VALUE "COBOL-IT".
77 STRING-2 PIC X(20) VALUE "OPEN SOURCE COBOL".
77 STRING-3 PIC X(42)
VALUE "A QUICK BROWN FOX JUMPED OVER THE LAZY DOG".
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
INSPECT STRING-1 TALLYING TALLY-CTR1 FOR ALL "O".
DISPLAY "THERE ARE " TALLY-CTR1 " O'S IN " STRING-1
LINE 5 COL 10.
INSPECT STRING-2 REPLACING ALL "E" BY "-".
DISPLAY STRING-2 LINE 6 COL 10.
INITIALIZE TALLY-CTR1.
INSPECT STRING-2 TALLYING TALLY-CTR1 FOR ALL "O"
REPLACING ALL "S" BY "+".
DISPLAY STRING-2 LINE 7 COL 10.
DISPLAY "THERE ARE " TALLY-CTR1 " O'S IN " STRING-2
LINE 8 COL 10.
DISPLAY STRING-3 LINE 10 COL 10.
DISPLAY "CONVERTING TO UPPER-CASE...." LINE 11 COL 10.
INSPECT STRING-3 CONVERTING
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO
"abcdefghijklmnopqrstuvwxyz".
DISPLAY STRING-3 LINE 12 COL 10.
DISPLAY "INSPECT-1 FINISHED!" LINE 15 COL 10.
ACCEPT DUMMY LINE 15 COL 30.
STOP RUN.