COBOL SORT n SORT MERGE Verbs are supported
COBOL SORT n SORT / MERGE Verbs are supported in COBOL n Three files are required INPUT FILE , OUTPUT FILE, SORTFILE (WORKFILE) n In FILE SECTION we need two FD STATEMENTS and one SD STATEMENT PROCEDURE DIVISION SORTFILE ON ASCENDING KEY sort-key-field ……. USING INFILE GIVING OUTFILE.
Files have to be declared in ENVIRONMENT DIVISION FD Statement is required for INPUT, OUTPUT FILES SD Statement is required for SORTFILE In the PROCEDURE DIVISION there is no need to OPEN/CLOSE any files SORT Statement is enough
SORT EXAMPLE SELECT INFILE ASSIGN TO DD 1. SELECT OUTFILE ASSIGN TO DD 2. SELECT SORTFILE ASSIGN TO DD 3. . . FILE SECTION. PROCEDURE DIVISION. FD INFILE. SORTFILE ON 01 INREC. ASCENDING KEY S-EC 02 I-EC PIC 9(4). USING INFILE ……. GIVING OUTFILE. FD OUTFILE. STOP RUN. 01 OUTREC. 02 O-EC PIC 9(4). RUNJCL DD STATEMENTS ……. //DD 1 DD DSN=…. … SD SORTFILE. //DD 2 DD DSN=…. 01 SORTREC. //SORTWK 01 DD SPACE=(CYL, (1, 1)) 02 S-EC PIC 9(4). ……. INSTEAD OF DD 3 , SORTWK 01 MUST BE GIVEN
MERGE n SYNTAX: MERGE SORTFILE ON ASCENDING KEY merge-key-field ……. USING INFILE 1 INFILE 2. . GIVING OUTFILE.
INPUT/OUTPUT PROCEDURES SYNTAX for Sort File SORTFILE ON ASCENDING KEY sort-key-field ……. INPUT PROCEDURE RTN 1 THRU RTN 2 OUTPUT PROCEDURE RTNX THRU RTNY. n INPUT PROCEDURE is used to execute a subroutine which will read and select the required records from the input file and write them in sort file n INPUTE PROCEDURE is executed before sorting activity is started
INPUT/OUTPUT PROCEDURES n Release Statement is associated with Input Procedure n Output Procedure is used to execute a subroutine which will read and select the required records from the sort file and write them in output file n Output procedure is executed after the sorting activity is over n Return statement is associated with output procedure
SORT WKFILE ON ASCENDING KEY WK-NAME INPUT PROCEDURE RTNA THRU RTNC GIVING OUTFILE END-SORT. ( EXAMPLE OF INPUT PROCEDURE ) RTNA. RTNB. RTNC. OPEN INPUT INFILE. READ INFILE AT END GO TO RTNC. IF …. MOVE INREC TO WKREC RELEASE WKREC END-IF GO TO RTNB. CLOSE INFILE.
SORT WKFILE ON ASCENDING WK-NAME USING INFILE OUPUT PROCEDURE RTNX THRU RTNZ END-SORT. ( EXAMPLE OF RTNX. RTNY. RTNZ. OUPUT PROCEDURE ) OPEN OUTPUT OUTFILE. RETURN WKFILE AT END GO TO RTNZ. IF …. MOVE WKREC TO OUTREC WRITE OUTREC END-IF GO TO RTNY. CLOSE OUTFILE.
STRING n String Statement is to concatenate (JOIN) data from several input variables / constants and copy to a single output variable n Source can be X, A, 9 type items, but not comp variables n Source can be 01 – 49 , 66 and 77 Level Items Delimited by Clause is needed to control the transfer of characters from the source
STRING SYNTAX STRING VAR/LIT DELIMITED BY SIZE/LITERAL INTO VAR-X [ ON OVER FLOW statements………. . ] [ WITH POINTER PTR ] END-STRING
EXAMPLE: 01 G 1. 02 …. 02 WS-FN 01 G 2. 02 WS- TEL …… 77 WS-PAY EX: STRING PIC X(10). PIC 9(8). PIC 9(5). 'MR. ' DELIMTED BY SIZE WS-FN DELIMITED BY ', ' '/' DELIMITED BY SIZE WS-TEL DELIMITED BY '#' '/' DELIMITED BY SIZE WS-PAY DELIMITED BY SIZE INTO OUT-REC. DISPLAY OUT-REC.
How the STRING Works n The STRING moves characters from the source variable into the destination variable from left to right. n When there are number of source variables, characters are moved from the leftmost variable.
How the STRING Works n When a WITH POINTER phrase is used its value determines the starting character position of the destination buffer n ON OVERFLOW condition occurs if the output buffer is insufficient to accommodate the concatenated input
UNSTRING n UNSTRING STATEMENT is to split the contents of a single input variable and transfer to multiple output variables n Source can be X, A, 9 type items, but not COMP variables
UNSTRING n Source can be variables or literals n Delimited by clause is needed to control the transfer of characters from the source n Generally the input variable must contain data with some delimiter to identify end of field
UNSTRING VAR/LIT DELIMITED BY LT 1 OR LT 2 …… INTO VAR-X VAR-Y VAR-Z [ ON OVER FLOW statements………. . ] [ WITH POINTER PTR ] END-UNSTRING INPUT STRING: EX: UNSTRING RAMA, 123 TNAGAR#23523511%600017@ IN-REC DELIMTED BY ', ' OR '#" OR '%' OR '@' INTO WS-FN WS-AD WS-TEL WS-PIN …. .
How the UNSTRING Works n The UNSTRING splits the input VARIABLE CONTENTS into several fields and copies them to separate destination variables n Delimiters in the input variable is used to identify the end of field. n WITH POINTER option, you can determine the starting position of the input buffer n The ON OVERFLOW condition occurs if the number of output variables are insufficient
INSPECT n INSPECT Statement is used to examine the contents of a variable for occurrence of a character / string n INSPECT with TALLY option can be used to count the number of occurrence of such character n INSPECT with REPLACE option can be used to replace the string / character with another string or character
INSPECT n INSPECT VAR-X TALLYING VAR-Y FOR ALL/LEADING /CHARACTERS char/string n INSPECT VAR-X REPLACING ALL /LEADING/FIRST char/string BY char/string n INSPECT VAR-X CONVERTING string-1 TO string-2
EXAMPLES INSPECT WS-NAME TALLYING CTR 1 FOR ALL SPACES INSPECT WS-AMT CTR 2 FOR LEADING '0' TALLYING INSPECT WS-NAME TALLYING CTR 3 FOR CHARACTERS BEFORE INTIAL SPACE. INSPECT WS-AMT REPLACING ALL SPACES BY INSPECT WS-DATE REPLACING ALL '/' BY '-' INSPECT WS-NAME CONVERTING 'abcdefghijkl' TO 'ABCDEFGHIJKL' '0'
REFERENCE MODIFICATION n SUBSTRING MANIPULATION IS CALLED REFERENCE MODIFICATION n SUPPORTED WITH MOVE STATEMENT n EX: WS-TEL CONTAINS 044 -2231212 MOVE WS-STD-TEL(5: 7) TO WS-A means move 7 characters from the 5 th position of input MOVE WS-STD-TEL(M: N) TO WS-A M and N are numeric variables MOVE WS-NAME(I-2: J+5) TO WS-A If i=5 , j=2, move 7 characters from 3 rd position
- Slides: 21