IBM MAINFRAMES COBOL Training Class7 Introduction to Strings
IBM MAINFRAMES COBOL Training Class-7
Introduction to Strings § § A STRING refers to a sequence of characters. String manipulation operations include § Finding a particular character/sub-string in a string, § Replacing particular character/sub-string in a string, § Concatenating strings and Segmenting strings. § All these functions are handled by three verbs, STRING, UNSTRING and INSPECT in COBOL. EXAMINE is the obsolete version of INSPECT supported in COBOL 74.
STRING § STRING verb is used to concatenate one or more strings. Example: MOVE 'SAHASRA' 'MAINFRAME' 'TRAINING' TO TO TO STRING FIRST-NAME DELIMITED ' ' DELIMITED MIDDLE-NAME DELIMITED ' ' DELIMITED LAST-NAME DELIMITED INTO WHOLE-NAME. DISPLAY WHOLE-NAME. FIRST-NAME. MIDDLE-NAME. LAST-NAME. BY BY BY SPACE SIZE SPACE
UNSTRING Verb is used to split one string into many strings. Example: MOVE 'SAHASRA MAINFRAME TRAINING' TO WHOLE-NAME. UNSTRING WHOLE-NAME DELIMITED BY SPACE INTO FIRST-NAME MIDDLE-NAME LAST-NAME. DISPLAY 'FIRST NAME: ' FIRST-NAME. DISPLAY 'MIDDLE NAME: ' MIDDLE-NAME. DISPLAY 'LAST NAME: ' LAST-NAME.
Example-2 01 WS-DATA PIC X(12) VALUE UNSTRING WS-DATA DELIMITED BY WS-FLD 1 DELIMITER IN WS-FLD 2 DELIMITER IN WS-FLD 3 DELIMITER IN END-UNSTRING. '/‘ '10/200/300/1'. INTO WS-D 1 COUNT IN WS-D 2 COUNT IN WS-D 3 COUNT IN Result: WS-FLD 1 = 10 WS-FLD 2 =200 WS-FLD 3=300 WS-C 1 = 2 WS-C 2=3 WS-C 3=3 WS-D 1 = '/' WS-D 2='/' WS-D 3 ='/‘ WS-C 1 WS-C 2 WS-C 3 ON OVERFLOW can be coded with STRING and UNSTRING. If there is STRING truncation the imperative statements followed ON OVERFLOW will be executed.
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. § 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. Unusually this is done by a MOVE 0 to whatever the counter is called.
TALLYING § 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 occurrences 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 FLDA = 256545_675 then after the INSPECT, MOVE 0 TO CTRA. INSPECT FLDA TALLYING CTRA FOR ALL ‘ 5’ BEFORE INTIAL SPACE. § CTRA would be equal TO 4.
Examples Example 2: FLDC = 16 AB 5_6 MOVE 0 TO CTRC. INSPECT FLDC TALLYING CTRC FOR CHARACTERS BEFORE INITIAL SPACE. Here the count of all characters before the first space is tallied. Example 3: FLDD = 00090020, MOVE 0 TO CTRD. INSPECT FLDD TALLYING CTRD FOR LEADING ZEROS. This statement counts all of the leading zeros in a field. Then CTRD is equal to 3.
REPLACING § This statement will replace a character or a group of characters with some other character. Example 1: FLDH = ‘ABCBDFB ’ INSPECT FLDH REPLACING ALL ‘B’ BY ‘G’. Ans AGCGDFG.
Example-2 & 3 Example 2: FLDI = ‘AAABBAAA’ INSPECT FLDI REPLACING CHARACTERS BY "X" BEFORE INITIAL "B". Ans This means that AAABBAAA will become XXXBBAAA. Example 3 FLDJ = ‘ACXDGXB’ INSPECT FLDJ REPLACING FIRST "X" BY "5". This will replace the first X with a 5. Ans AC 5 DGXB.
Reference Modification § § Reference modification is used to retrieve/replace the set of characters from a string. When we want to move a part of text from one string to another string we make use of reference modification move. Syntax: WS-VAR 1(STARTING POSITION: LENGTH) 01 WS-A MOVE PIC X(7) WS-A(1: 5) WS-A(3: 3) VALUE TO TO WS-B. ‘SAHASRA’.
ON SIZE ERROR § ON SIZE ERROR clause is coded to trap size errors in arithmetic operation. § Example: ADD A TO B ON SIZE ERROR DISPLAY ‘ERROR!’. § If the condition occurs, the code in the ON SIZE ERROR phrase is performed, and the content of the destination field remains unchanged. If the ON SIZE ERROR phrase is not specified, the assignment is carried out with truncation. § § There is no ON SIZE ERROR support for the MOVE statement Fixed-point overflow. Division by 0 can also be trapped.
RETURN-CODE § RETURN-CODE is a special register and its content is moved to register 15 when the control is given back to OS. So move the return code to this register in the program. § § § RETURN-CODE should not be declared in your program. Ex: MOVE 1000 to RETURN-CODE. MAX 4095 This sets return code as 1000 for the step as MAXCC that executes this program.
Return Code and Abend Code Program WORKING-STORAGE SECTION. 01 WS-A PIC 9(2). 01 WS-B PIC 9(2). 01 WS-C PIC 9(2). 77 WS-AB-CODE PIC S 9(4) COMP. PROCEDURE DIVISION. MAIN-PARA. ACCEPT WS-B. COMPUTE WS-C = WS-A + WS-B ON SIZE ERROR MOVE 100 TO WS-AB-CODE. MAX 4095 CALL 'ILBOABN 0' USING WS-AB-CODE. DISPLAY WS-C. STOP RUN.
Thank You Polsani Anil Kumar
- Slides: 16