COBOL .

COBOL Inspect

Working-Storage for Examples 1 and 2

000340 01  CAT-TYPE    PIC X(15) VALUE 'KALIKO'.
000350 01  DOG-TYPE    PIC X(15) VALUE 'SCHNAUZER'.    
000350 01  Z-COUNT     PIC 9.

Inspect Example Number 1 - Initialize a PIC 9 and a PIC X field

004310
004320     INSPECT CAT-TYPE
004330        REPLACING ALL 'K' BY 'C'.
004440
This will change KALIKO to CALICO.

Inspect Example Number 2 - Counting occurances of a value.

004310
004320     INSPECT DOG-TYPE
004330        TALLYING Z-COUNT FOR ALL 'Z'.
004440
This will give Z-COUNT a value of 1.

And a reader writes in - I suggest a more practical example of INSPECT

01 MY-DATE PICTURE 99/99/99.
INPSECT MY-DATE REPLACING ALL "/" BY "-". 
01 MY-MONEY PICTURE $$$,$$$.99.
INSPECT MY-MONEY REPLACING LEADING " $" BY "Fr".
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by TheMadProfessor on 2013-08-09 Rate this Comment

In regards to your question, I'm a bit confused. Did you mean replacing "&" with "and"? If so, then you are correct that INSPECT/EXAMINE won't work since the lengths must be equal. The only way I can think of offhand would be perform a loop like this (assumes that the replacement is done differently in some circumstances - handles text like "A&E Network" to result in "A and E Network" instead of "AandE Network") I've replaced spaces in literals with asterisks and indented with periods for clarity's sake:

MOVE 'N' TO done-flag
MOVE nn TO text-limit (where nn length of text-string)
PERFORM UNTIL done-flag = 'Y'
....MOVE +0 TO char-count
....MOVE +4 TO char-offset
....INSPECT text-string TALLYING char-count FOR CHARACTERS BEFORE INITIAL '*&*'
....IF char-count EQUAL text-limit (true if didn't find '*&*', so now look for '&')
........MOVE +0 TO char-count
........MOVE +2 TO char-offset
........INSPECT text-string TALLYING char-count FOR CHARACTERS BEFORE INITIAL '&'
....END-IF
....IF char-count EQUAL text-limit (true if neither string found, so we're done)
........MOVE 'Y' TO done-flag
....ELSE (found one, so replace it)
........STRING text-string(1:char-count), '*and*', text-string(char-count + char-offset:text-limit) INTO text-work
........MOVE text-work TO text-string
....END-IF
END-PERFORM

Also, a note about the code replacing '*$' with 'Fr'. Since LEADING was specified, the replacement would occur only if this was the first two characters in the edited result. I think you would either want to omit LEADING in this instance or change the PICTURE clause so the dollar sign is not floating (most likely the former).

Comment by Anonymous on 2013-05-22 Rate this Comment

Are you sure you specified "_$" (where _ signifies a space) and not just "$"? If you did and are still getting the error, it may be a limitation of MF Cobol, not Cobol II.

Comment by archive on 2007-08-03 Rate this Comment

I went to your site an I was looking at the inspect clause examples and the example that states

01 MY-MONEY PICTURE $$$,$$$.99. INSPECT MY-MONEY REPLACING LEADING " $" BY "Fr".

Is not allowed in cobol, the values "fr" can only be one character. I am using the microfocus cobol complier and the compiler won't let me by pass this, it says "Operand <data-name> has wrong size" .

I would like to ask u a question, I have a variable that has a value of "for stunts & sfx", I would like for the program to find the "&" and replace it w/ "&" , can u suggest any possible solutions. I tried the inspect and examine neither of these clause the ability to insert the new value that I want, it just gives me size error.

Thanks in advance!

 
Sign in to comment on COBOL Inspect.