The INSPECT statement

12 Sep

The INSPECT verb has several options: CONVERTING, REPLACING, and TALLYING.

Here’s an example of the CONVERTING format:

Let’s say that we have a field called FAVORITE-FOOTBALL-TEAM that contains this string:  Pittsburgh Steelers.    What if our application requires that the name be in upper case?  The INSPECT verb can convert it very easily, just like this:

After this statement is executed, the value of our field is PITTSBURGH STEELERS



Here’s an example of the REPLACING format:

We have a field called PART_NUMBER that looks like this:


We’ll assume that at some point, a clerk having a bad day inadvertently keyed in this part number using asterisks instead of dashes, and now we’re tasked with replacing them.   This can be easily accomplished using the INSPECT REPLACING verb like this:

After this statement is executed, the value of our field is SOME-PART-NUMBER


In another example, we have a field called FRUSTRATIONS containing the string RUSH HOUR.   If we execute this INSPECT REPLACING statement, we can turn RUSH HOUR into RASH HOUR  (better contact a dermatologist).

Notice that because we said INITIAL “U”, the statement only changed the “U” to “A” in the word RUSH, and not in the word HOUR.


And now an example of the INSPECT TALLYING format:

Tallying can be a little more involved.  When using INSPECT TALLYING, you tally into a field that is a counter.   Always remember to initialize this counter to zero before the inspect is done!

You may have a need to count the leading zeroes in a field.  We have a field called TEXT-FIELD that contains the numeric value of 000780395.   We can find the number of leading zeroes like this:

After this statement is executed, the value of CTR will be 3.

If we needed to know how many zeroes exist in the same field (not just leading zeroes), we could do this:

After this statement is executed, the value of CTR will be 4.


Now let’s say, for example, that we have a 20 character field that contains five spaces, followed by five numeric digits, and having 10 spaces at the end.  Maybe these five numeric digits represent a misplaced zip code that we have to extract.  So our field looks like this: _____15219__________, (each underscore represents a space), and we need to find the zip code   This one is a little trickier…   If you get lost, review my post on positional parameters.

So let’s review this one, as it can be a little intimidating.  First, I’ve shown you the fields involved as they are defined in the WORKING-STORAGE SECTION.   In the PROCEDURE DIVISION, we first initialize all of our counters to zero.  DON’T FORGET THIS STEP!!!   After the first INSPECT is executed, the value of LEADING-CTR will be 5.  After the next INSPECT is executed, the value of TRAILING-CTR will be 10.   We then sum LEADING-CTR and TRAILING-CTR into SPACE-CTR, making that value 15.   Since we know that our field LONG-FIELD is 20 characters long, we then subtract SPACE-CTR from 20 giving POPULATED.  The value of POPULATED is 5, so we know that five characters of the 20 character field actually have a value other than a space.  Now for the tricky part:

MOVE LONG-FIELD (LEADING-CTR + 1:POPULATED) TO ZIP-CODE.     Remember that the value of LEADING-CTR is 5, because there are five leading spaces.  That means that our zip code begins in position 6.  (LEADING-CTR + 1).  The value of POPULATED is 5, so this statement causes us to move position 6 for a lengh of 5 to the field ZIP-CODE.   Not too tough when we break it down, right?

Are there other ways you could accomplish this?   ABSOLUTELY!     But my purpose here is to demonstrate the INSPECT statement, and this is a good example.   Happy INSPECTING!


Leave a Reply

Your email address will not be published. Required fields are marked *