INSPECT

The INSPECT verb has two options, TALLYING and REPLACING. You can do one or the other or both. If both are done, the TALLYING IS DONE BEFORE THE replacing. Some versions of the INSPECT require that all the literals be in quotes. This may call for a redefination if the field is numeric.

In using the TALLYING format of the INSPECT, you are tallying into a field that is a counter. This field must be initialized to 0 before the inspect is done. Unsually this is done by a MOVE 0 to whatever the counter is called.

The TALLYING option of the INSPECT has multiple options - in fact, some versions have options beyond those required by the COBOL specifications:

- You can tally ALL of something
- You can tally just the LEADING occurances of something
- You can just tally CHARACTERS
- You can tally BEFORE or AFTER an INITIAL specified character

__Example #1: __This statement counts the number of occurrences of the digit 5 until the first space is encountered. If FLA = 256545 67 then after the INSPECT, CTRA would equal 3.

MOVE 0 TO CTRA.

INSPECT FLDA TALLY CTRA

FOR ALL "5" BEFORE INTIAL SPACE.

__Example #2: __This statement counts the number of spaces in the field FLDB.

If FLB = 4 5 6 1 2 then after the INSPECT, CTRB would equal 4 since there is a space between each number.

MOVE 0 TO CTRB.

INSPECT FLDB TALLYING CTRB

FOR ALL SPACES.

__Example #3:__ In this example, the count of all characters before the first space is tallied. If FLDC is equal to 16AB5 6 then CTRC will contain the number 5 since 1, 6, A, B, and 5 proceed the space before the 6.

MOVE 0 TO CTRC.

INSPECT FLDC TALLYING CTRC

FOR CHARACTERS BEFORE INITIAL SPACE.

__Example #4:__ This statement counts all of the leading zeros in a field. Embedded or trailing zeros are counted. IF FLDD is equal to 00090020, then CTRD is equal to 3.

MOVE 0 TO CTRD.

INSPECT FLDD TALLYING CTRD

FOR LEADING ZEROS.

__Example #5:__ This statement counts all zeros in the field. IF FLDE is equal to 00090020, then CTRE will be equal to 6.

MOVE 0 TO CTRE.

INSPECT FLDE TALLYING CTRE

FOR ALL ZEROS.

__Example #6:__ This statement tallies for all zeroes that proceed the initial 2 In this case, CTRF will be 5. FLDF is equal to 00090020.

INSPECT FLDF TALLYING CTRF

FOR ALL ZEROS BEFORE INITIAL 2.

__Example #7:__ This statement counts the spaces before the number 5. In this example CTRG will be 3. FLDG has three spaces and then the number 1257 ( 1257).

INSPECT FLDG TALLYING CTRG

FOR LEADING SPACE BEFORE INITIAL 5.

__Example #8:__ This statement will replace all B with G. FLDH will start as ABCBDFB and will become AGCGDFG.

INSPECT FLDH REPLACING ALL "B" BY "G".

__Example #9:__ This replace will change all A to X. This means that AAABBAAA will become XXXBBAAA.

INSPECT FLDI REPLACING CHARACTERS BY "X"

BEFORE INITIAL "B".

__Example #10:__ This will replace the first X with a 5. There ACXDGXB will become AC5DGXB.

INSPECT FLDJ REPLACING FIRST "X" BY "5".