Saturday 10 August 2013

COBOL INSPECT | INSPECT IN COBOL With Examples.

COBOL INSPECT, INSPECT in COBOL
COBOL INSPECT

In a various programming language, string manipulation is achieved by using multiple functions for example in Java, the methods of a String class is used for string manipulation. Similarly, COBOL also uses a library of string manipulation functions, but most string manipulation is done using reference modification and the three string-handling verbs: STRING, UNSTRING, and INSPECT.

In this tutorial, you will get familiar with the string-handling verbs INSPECT. The COBOL INSPECT verbs allow you to count and replace characters, and concatenate and split strings. You are then introduced to reference modification, which lets you treat any string as an array of characters. Finally, you learn about the intrinsic functions used for string and date manipulation.

What is the COBOL INSPECT statement?

INSPECT in COBOL is primarily used for manipulating text/string. The COBOL INSPECT statement lets you count characters in a field or replace characters in a field. The INSPECT statement has four different formats that vary based on the use of the Tallying, Replacing, and Converting clauses. You’ll learn about each of the COBOL INSPECT statement formats with examples.

COBOL INSPECT statement example: 

The following visual diagram shows an example of INSPECT statement results.

COBOL INSPECT
COBOL INSPECT statement.

Format 1: INSPECT ‥ TALLYING.

INSPECT‥TALLYING counts the number of occurrences of a character in a string. You can use the Inspect statement with the Tallying clause to count specific characters in the field that’s inspected. That field can be alphabetic, alphanumeric, or numeric with Display usage. 

When you code CHARACTERS in the Tallying clause, it refers to all of the characters in the field that are being inspected. In contrast, ALL refers to all occurrences of the specified character or characters, and LEADING refers to all occurrences of the specified character or characters at the start of the field.

Since the count fields aren’t set to zero each time the Inspect statement is executed, you must set them to the starting values that you want before you issue this statement. The examples above assume that the count fields have been set to zero.

The non-numeric literals that you use in the Tallying clause should be consistent with the data type of the field that’s being inspected.

Note: If a character is identified by more than one phrase in the Tallying clause, it is only counted the first time it is encountered. A character isn’t counted more than once.

INSPECT TALLYING in COBOL Syntax.


COBOL INSPECT TALLYING

INSPECT TALLYING in COBOL with Example. 

Consider that WS-DATA-FLD and WS-CNTR are defined in  the Working-Storage Section as follows:

77 WS-CNTR               PIC 9(01)  VALUE ZERO. 
01 WS-DATA-FLD           PIC X(11)  VALUE SPACES. 
01 WS-BLOG-NME           PIC X(11)  VALUE SPACES. 
01 WS-WRK-FLD            PIC X(11)  VALUE SPACES.
. . . . 

MOVE '00ACADEMY00'        TO WS-DATA-FLD.
INSPECT WS-DATA-FLD TALLYING WS-CNTR FOR LEADING “0” 
REPLACING FIRST “A” BY “2” AFTER INITIAL “C”.

Result: 
Initial Value : 00ACADEMY00
Counter Value : 2
After Value   : 00AC2DEMY00


COBOL INSPECT EXAMPLE. 

MOVE 'MAINFRAME FORUM'    TO WS-BLOG-NME.
INSPECT WS-BLOG-NME TALLYING WS-CNTR FOR ALL "MA"

Result: 
Initial Value : 'MAINFRAME FORUM'
Counter Value : 1

COBOL INSPECT TALLYING multiple values. 

MOVE '00$C,DE. 00'        TO WS-WRK-FLD.
INSPECT WS-WRK-FLD TALLYING WS-CNTR FOR ALL "$" ALL "," ALL "."
REPLACING LEADING " " BY "0".

Result: 
Initial Value : '00$C,DE. 00'
Counter Value : 3
Final Value   : '00$C,DE.000' 

Consider that WS-FLD and WS-CNTR are defined in  the Working-Storage Section as follows:

77 WS-FLD                PIC X(09)  VALUE “MANFRAME”.

01 WS-CNT                PIC 9(01)  VALUE 0.

Here are some examples of INSPECT statements and their results with the data above, assuming that COUNT-0 always begins with the value 0:

INSPECT WS-FLD TALLYING WS-CNT FOR ALL "FR"

INSPECT WS-FLD TALLYING WS-CNT FOR ALL CHARACTER BEFORE "E"

INSPECT WS-FLD TALLYING WS-CNT FOR LEADING "E"

INSPECT WS-FLD TALLYING WS-CNT FOR LEADING "F" AFTER "M"

INSPECT WS-FLD TALLYING WS-CNT FOR ALL CHARACTER 
AFTER "F" BEFORE "M"

Combining CHARACTERS, ALL, and LEADING.

If you combine the CHARACTERS, ALL, and LEADING phrases in the same INSPECT TALLYING statement, the inspection proceeds character by character, applying each inspection criterion in succession. The order of the inspection tests is the order in which you specify them in the INSPECT statement. Once a match is found, the current position in ident-1 is advanced, and the comparison begins again with the first operand.

Consider that WS-FLD and WS-CNTR are defined in  the Working-Storage Section as follows:

77 WS-DATA         PIC X(09) VALUE “HEELWHEEL”.
77 WS-CNT-0        PIC 9(02) VALUE  ZERO. 
77 WS-CNT-1        PIC 9(02) VALUE  ZERO. 
77 WS-CNT-2        PIC 9(02) VALUE  ZERO. 

INSPECT WS-DATA TALLYING
    WS-CNT-0 FOR ALL “E”,
    WS-CNT-1 FOR LEADING “W” AFTER “L”
    WS-CNT-2 FOR CHARACTERS.
DISPLAY "WS-CNT-0: WS-CNT-0.
DISPLAY "WS-CNT-1: WS-CNT-1.
DISPLAY "WS-CNT-2: WS-CNT-2.

The INSPECT TALLYING statement counters have the following values:

WS-CNT-0: 4 
WS-CNT-1: 
WS-CNT-2: 4     

Note: Can you guess why WS-CNT-2 is 4. If you know the explanation then leave ur comment in the comment section. 

Format 2: INSPECT ‥ REPLACING (how the Inspect statement with the Replacing clause works.)


INSPECT‥REPLACING replaces characters in the string with a replacement character. This statement replaces specific characters in the field that’s being inspected.

As you code the Replacing clause, you need to remember that this statement only replaces characters. It can’t insert or delete characters. So when you code a phrase to replace a single character, you must replace it with just a single character. And if you code a phrase to replace two characters, like CR or DB, you must replace them with two characters, like two spaces.

Note: The non-numeric literals that you use in the Replacing clause should be consistent with the data type of the field that’s being inspected.


COBOL INSPECT REPLACING in COBOL Syntax.


INSPECT statement with REPLACING phrase
COBOL INSPECT REPLACING


In the following INSPECT REPLACING example, the INSPECT REPLACING statement is used to scrutinize and replace characters in the data item/variables.


INSPECT REPLACING in COBOL with Example. 

77 WS-CNTR2        PIC S9(03) VALUE +0.
77 WS-DATA         PIC  X(09) VALUE “00ACADEMY00”. 
........
INSPECT WS-DATA-2 TALLYING COUNTR2
        FOR LEADING “0” REPLACING 
        FIRST “A” BY “2” AFTER INITIAL “C”

Result: 
Initial Value : '00ACADEMY00'
Counter Value :  2
Final Value   : '00AC2DEMY00' 

COBOL INSPECT REPLACING low-values

01 WS-DATA-2       PIC  X(08)  VALUE  LOW-VALUES. 
....

INSPECT WS-DATA-2 REPLACING LOW-VALUES BY SPACES


INSPECT in COBOL to replace quotation mark (“) in the string.

01 WS-DATA-3       PIC  X(08)   VALUE '453”XYZ' 
.......

INSPECT WS-DATA-3 REPLACING 
        CHARACTERS BY ZEROS BEFORE INITIAL QUOTE

Result: 
Before Value : '453”XYZ'

After  Value : '000”XYZ'

Note: Each character in the WS-DATA-3 variable that precedes the first occurrence of a quotation mark (“) is replaced by the digit 0. 

Format 3: COBOL INSPECT TALLYING & REPLACING clauses.


The third format of the Inspect statement includes both the Tallying and Replacing clauses. You can use the Inspect statement with the Tallying and Replacing clauses to both count and replace characters in a field. This is the same as first issuing an Inspect Tallying statement and then issuing an Inspect Replacing statement.

So if you understand how these clauses are used in separate statements, you shouldn’t have any trouble using them in a single statement because they work the same way.

COBOL INSPECT TALLYING REPLACING Syntax
COBOL INSPECT TALLYING REPLACING Syntax

INSPECT Tallying and REPLACING in COBOL Syntax

INSPECT statement with TALLYING and REPLACING phrases

INSPECT Tallying and REPLACING in COBOL Example

INSPECT DATA-3 REPLACING CHARACTERS BY ZEROS BEFORE INITIAL QUOTE


Format 4: INSPECT ‥ CONVERTING


INSPECT‥CONVERTING seems very similar to INSPECT‥REPLACING but actually works quite differently. It is used to convert one list of characters to another list of characters on a character-per-character basis.

If all you want to do is convert one or more characters in a field to other characters, you can do that with the Converting clause more easily than you can with the Replacing clause. Here, you use one literal to identify all of the characters you want to replace and another to identify the characters that you want to replace them with on a one-to-one basis.

One traditional use of the Inspect statement with the Converting clause has been to convert all the lowercase letters in a field to uppercase, or vice versa. That requires a simple statement like the one shown in this figure. If the Lower-Case and Upper-Case functions are available on your compiler, though, it’s easier to use them.

COBOL INSPECT CONVERTING syntax
COBOL INSPECT CONVERTING syntax

INSPECT CONVERTING in COBOL Syntax

INSPECT statement with CONVERTING phrase

INSPECT CONVERTING in COBOL Example

01 DATA-4 PIC X(11)
. . . . INSPECT DATA-4 CONVERTING ”abcdefghijklmnopqrstuvwxyz“ TO 
                                  ”ABCDEFGHIJKLMNOPQRSTUVWXYZ“ 
        AFTER INITIAL ”/“ 
        BEFORE INITIAL”?“

Topitrick: COBOL Inspect Video. 


Summary. 

Now that you’ve completed this tutorial, you should be able to use any of the features for working with characters in your programs. Since most business programs work with records and fields, not characters, you won’t use these features that often. But every professional programmer needs to use them for special editing and conversion routines.


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™

No comments:

Post a Comment

New In-feed ads