ContentsIndexPreviousNext

Code Examples

Code example 1:

Use INSPECT to count the number of occurrences of a character or string:

01  CHAR-COUNT  PIC 99 VALUE 0.

*count all "b"s
INSPECT INPUT-ITEM
   TALLYING CHAR-COUNT FOR ALL "B".

Value of INPUT-ITEM
CHAR-COUNT
BB44@#AL23#AL88#xx#CC
2
BB@#BBBB#CCCC#xx
6
B@#BB#
6

*count all "#"s found after the first "@"
*and before the first "x"
INSPECT INPUT-ITEM
TALLYING CHAR-COUNT FOR ALL "#"
AFTER "@" BEFORE "x".

Value of INPUT-ITEM
CHAR-COUNT
BB44@#AL23#AL88#xx#CC
3
BB@#BBBB#CCCC#xx
3
B@#BB#
2

*count all characters
INSPECT INPUT-ITEM
TALLYING CHAR-COUNT FOR CHARACTERS.

Value of INPUT-ITEM
CHAR-COUNT
BB44@#AL23#AL88#xx#CC
24
BB@#BBBB#CCCC#xx
19
B@#BB#
9

Code example 2:

Use INSPECT to replace matching characters or strings:

INSPECT NAME-LIST REPLACING
*if the first characters in the string are "a"
*replace the "a"s with "A"s
   LEADING "a" BY "A"
*replace all "T"s found after the first "/"
*with "t"
   ALL "T" BY "t" AFTER "/"
*replace all "/"s with ":"
   ALL "/" BY ":"
*after the first "-" replace all characters
*in the string with "Z"
   CHARACTERS BY "Z" AFTER "-".

Input value NAME-LIST
Output value NAME-LIST
TED/TRAVIS/UREY/VENNEY
TED:tRAVIS:UREY:VENNEY
aVERY/BLAZE/TERI
AVERY:BLAZE:tERI
MAVIS-GUS-HAL-WESTON
MAVIS-ZZZZZZZZZZZZZZ

Code example 3:

Use INSPECT to both tally and replace characters or strings:

INSPECT PART-LIST
*count all "P-"
   TALLYING P-COUNT FOR ALL "P-"
*replace all "xx" by "__"
   REPLACING ALL "xx" BY "__".

Value of PART-LIST
P-COUNT
P-BOLTxxP-WASHERxxP-NUT
3

Input value PART-LIST
Output value PART-LIST
P-BOLTxxP-WASHERxxP-NUT
P-BOLT__P-WASHER__P-NUT

Code example 4:

Use INSPECT/CONVERT to convert every occurrence of the specified characters in the input string (equivalent to a series of REPLACE ALL phrases). The list of characters following the words CONVERT and TO is not a string, but, rather, a list of individual characters. INSPECT/CONVERT replaces every occurrence of each character in the CONVERT list with the character in the matching ordinal position of the TO list. AFTER and BEFORE can be used to bracket a portion of the source string.

*convert all occurrences of:
*"-" to "0", "l" to "L",
*"a" to "A" and "/" to ":"
INSPECT PART-LIST
   CONVERTING "-la/" TO "0LA:".

This INSPECT/CONVERT statement is equivalent to:

INSPECT PART-LIST
   REPLACING ALL "-" BY "0"
             ALL "l" BY "L"
             ALL "a" BY "A"
             ALL "/" BY ":".

Input value PART-LIST
Output value PART-LIST
Yla-1/Yla-2/Yla-3/Yla21
YLA01:YLA02:YLA03:YLA21