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-n
is a numeric data item.identifier-n
is a data element, literal, or data returned from a function call.
General Rules:
- The
TALLYING
clause 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-1
as defined by the after before clause. - The
CHARACTERS
phrase causes a simple count of the number of characters in the named identifier to be tallied, and stored innumeric-data-1
. - The
ALL
phrase causes a count of the number of iterations ofidentifier-2
in the named identifier to be tallied, and stored innumeric-data-1
. - The
LEADING
phrase causes a count of the number of contiguous iterations ofidentifier-2
, starting at the left most position in the named identifier. - The
FIRST
phrase causesnumeric-data-1
to be incremented if/when the first iteration ofidentifier-2
is located in the named identifier. - The
TRAILING
phrase 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
REPLACING
clause is designed to identify a string of characters inidentifier-1
or in a subset of the characters inidentifier-1
as defined by the after before clause, and replace them with an alternative string of characters of the same size. - The
CHARACTERS
phrase causes every character inidentifier-1
to be replaced byidentifier-4
. When using theCHARACTERS
phrase,identifier-3
is an alphanumeric data item that is a single character. - The
ALL
phrase causes all instances ofidentifier-3
insideidentifier-1
to be replaced byidentifier-4
. - The
LEADING
phrase causes all contiguous iterations ofidentifier-3
insideidentifier-1
, starting at the left most position inidentifier-1
to be replaced byidentifier-4
. - The
FIRST
phrase causes the first iteration ofidentifier-3
to be replaced byidentifier-4
. - The
TRAILING
phrase causes all contiguous iterations ofidentifier-3
insideidentifier-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
, andTRAILING
phrases,identifier-3
andidentifier-4
must be strings of the same length.after-before
phrase 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-BEFORE
phrase specifies the end-point or beginning-point in the named identifier that will be parsed in theINSPECT
statement. - The
BEFORE
phrase causes theINSPECT
/TALLYING
/REPLACING
statement 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-5
is located. - The
AFTER
phrase causes theINSPECT
/TALLYING
/REPLACING
statement to locate a parsing beginning point in the named identifier. The named identifier is scanned from right to left. If anidentifier-5
is located, it marks the parsing beginning-point, and parsing continues to the end of the identifier.CONVERTING
phrase 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
CONVERTING
phrase dictates a conversion of characters withinidentifier-1
according to rules established by the ordinal positions of characters inidentifier-2
andidentifier-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 CONVERTING
is 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-1
to its corresponding upper-case letter.after-before
phrase 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-BEFORE
phrase specifies the end point or beginning point in the named identifier that will be parsed in theINSPECT
statement. - The
BEFORE
phrase causes theINSPECT
/CONVERTING
statement 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-6
is located . - The
AFTER
phrase cases theINSPECT
/CONVERTING
statement to locate a parsing beginning-point in the named identifier. The named identifier is scanned from right to left. If anidentifier-6
is 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.