


1. How the matching process works:
In all formats of the INSPECT statement there must be a set of specified "match" values. The match values may be single characters or strings, or a mix of both. The match values are the arguments specified after the TALLYING/FOR, REPLACING, or CONVERTING key words, and before the BY or TO key words (for further clarification see the example that follows).
INSPECT attempts to locate the match values in the source data item. The match values are searched for, in order of appearance in the code, at each position in the source string. Inspection starts at the leftmost character of the source data item and proceeds, character by character, to the rightmost character.
Match process example:
INSPECT source-data-item
REPLACING "cd" BY "QP"
"e" BY "T"
"f" BY "V".
Source data item: "abcdefg"
Set of match values: "cd"
"e"
"f"
The search begins at the leftmost character:
"abcdefg" ^
The first value in the match set is "cd". The first element, "c", is compared to the value "a" for a match. No match. The second value in the match set is "e". "e" is tested for a match with "a". No match. The third value in the match set is "f". "f" is tested for a match with "a". No match. There are no untested values remaining in the match set. The current position in the source data item is advanced one position to the right.
"abcdefg" ^
The sequential testing of the members of the match set to the value of the current position in the source data item is repeated. None matches. The current position in the source data item is advanced one position to the right.
"abcdefg" ^
The first match value is "cd". "c" is tested for a match with the current position in the source data item and there is a match. "d" is tested against the value of 1 + the current position ("d") and, again, there is a match. There are no more characters in the match value ("cd"). The first value in the match set results in a complete match at the current position in the source data item. Remaining values in the match set are not tested. The specified REPLACING action is performed. The current position in the source data item is advanced the length of the match value ("cd"), two places to the right.
"abQPefg"
^
Sequential testing of the members of the match set to the value of the current position in the source data item is repeated. The second member of the match set, "e", is a match. The REPLACING action is performed and the current position in the source data item is advanced the length of the match value, one position to the right.
"abQPTfg"
^
Notice that, if we had replaced "cd" with "ef" instead of "QP", the "e" and "f" would not be subsequently replaced with "T and "V".
Sequential testing of the members of the match set to the value of the current position in the source data item is repeated. The third member of the match set, "f", is a match. The REPLACING action is performed and the current position in the source data item is advanced the length of the match value, one position to the right.
"abQPTVg"
^
Sequential testing of the members of the match set to the value of the current position in the source data item is repeated. None matches. The current position in the source data item is the rightmost character; therefore, the inspection halts.
For a more concise description of the matching process see General Rule 5.
2. All replacement actions must replace the same number of characters as matched. The source data item may not change in size.
3. Use AFTER and BEFORE to bracket (define) a substring in the source data item.
4. Use ALL to match all occurrences of the specified value in the string.
5. Use CHARACTERS to match every character in the source data item that hasn't already been matched. Because CHARACTERS matches all elements of the source data item, CHARACTERS usually appears as the last phrase in the statement.
6. Use LEADING to find the leading occurrence, or set of leading contiguous occurrences, in the source data item.
7. Use TRAILING (ACUCOBOL-GT extension) to find the rightmost occurrence, or set of contiguous occurrences, in the source data item. If a TRAILING occurrence is found, a right to left scan of the source data item is made to find contiguous occurrences.
8. Use FIRST to specify a match of the first occurrence only.
9. Many COBOL programming texts caution against writing involved and complicated INSPECT statements because complex statements are difficult to understand and maintain.