Click for Dennis & Schwab Inc. Sponsor Message

News Headlines
Tech Headlines
Plan Headlines
Front Page
Search the NewsWire
Got a tip you'd like to share or a question about how to use COBOL on the 3000? Send them or your comments to me. Include your name your company, or just post anonymously. We'll reply to all your messages and do our best to answer questions.

Ron Seybold, Editor In Chief

Sort Procedures

By Shawn M. Gordon

I’ve recently had to deal with sorting at various levels and got into some conversations about the best approach. There are a couple of issues with sorting. First, is it just a small table that you want to sort in memory? If it is, then a bubble or shell sort might be the way to go, in order to avoid writing the data to a file. I will cover these techniques in a later column.

For sorting files you have several options. You can call the sort intrinsics directly, which is nasty. You can take advantage of the FD and SD and call the SORT verb in your code, or use the variation of the SORT verb with INPUT/OUTPUT procedures using RELEASE and RETURN. I am going to cover the last technique, because it is the one that is most useful and least used.

I remember when I first saw RELEASE/RETURN, a friend of mine was learning COBOL and he was asking me about it. I immediately responded that there was no such thing, and what the heck was his teacher telling him? I finally decided to look it up — and boy, was my face red. But I didn’t feel too bad, because the description in the manual is so awful. Check this out:

“The RETURN statement can be used in an output procedure of a SORT or MERGE statement. It cannot be used in any other type of procedure. For more information on the RETURN statement refer to Chapter 12, “SORT/MERGE Operations”.

“The RELEASE statement can be used in an input procedure of a SORT statement to transfer records from your program to the initial phase of the sort operation. For more information on the RELEASE statement refer to Chapter 12, 'SORT/MERGE Operations.' ”

Chapter 12: SORT/MERGE Operations

“The sort/merge capabilities of HP COBOL II allow you to sort one or more files of records, or to combine two or more identically ordered files of records one or more times within a given execution of a COBOL program.

“Additionally, you have the ability to specially process individual records by input or output procedures that are part of the sort or merge operation. For a sort operation, this special processing may be applied before as well as after the records have been sorted. For a merge operation, this processing may be applied after the records have been combined. The RELEASE and RETURN statements are used in these input procedures to release or return records that are to be, or have been sorted or merged.”

Now if that explains how to do it, then I’m impressed with whoever figures it out. I finally found out how to do this from a fine programmer by the name of Alan Oshiro at a job in the late 80s, who I have not been able to track down for many years now. In any case, you all get to take advantage of that time Alan spent with me. Figure 1 below shows how to use RELEASE and RETURN.

Figure 1


                INPUT-OUTPUT SECTION.
                FILE-CONTROL.
                    SELECT REPORT-FILE          ASSIGN TO "LP".
                     SELECT SORT-FILE              ASSIGN TO
"SORTWK".
                DATA DIVISION.
                FILE SECTION.

                FD REPORT-FILE.
                01 REPORT-REC                PIC X(132).

                SD SORT-FILE.
                01 SORT-REC.
                    03 SORT-KEY.

                PROCEDURE DIVISION.

                A1000-INIT.
                     SORT SORT-FILE ASCENDING KEY SORT-KEY
                                INPUT   PROCEDURE A2000-READ-FILE
                                OUTPUT PROCEDURE A3000-PRINT-REPORT.
                     STOP RUN.

                A2000-READ-FILE.
                       PERFORM A2100-READ-FILE THRU A2100-EXIT UNTIL EOF.

                A2100-READ-FILE.
                     RELEASE SORT-REC.
                     DBGET MYRECORD.
                     IF END-OF-FILE
                          SET EOF TO TRUE.
                A2100-EXIT.  EXIT.

                A3000-PRINT-RECPORT
                     RETURN SORT-FILE
                            AT END
                          MOVE "NO RECORDS QUALIFIED" TO REPORT-REC
                          WRITE REPORT-REC AFTER 2
                        GO TO A3000-EXIT.
                     MOVE SORT-KEY TO PREV-KEY.
                     MOVE "N" TO SW-EOF.
                     PERFORM A3100-READ-SORT-FILE THRU A3100-EXIT UNTIL EOF.
                A3000-EXIT.  EXIT.

                A3100-READ-SORT-FILE.
                     MOVE LINE-1 TO LP-LINE
                     WRITE REPORT-REC FROM LP-LINE.
                     MOVE SORT-KEY TO PREV-KEY.
                     RETURN SORT-FILE
                         AT END
                        SET EOF TO TRUE.
                A3100-EXIT.  EXIT.

Now the code fragment in Figure 1 is a bit light on details, but should hopefully give you enough to get started. I suggest that you play around with this technique a bit and make sure you understand how it is working. Like I said, it is (or at least the last time I checked) just slightly faster than using the standard SORT verb in COBOL.

As always, I welcome your input to this column. Earn yourself a free 3000 Always Online hat for tips you submit, either to the NewsWire or to me at shawn@smga3000.com.

Shawn Gordon, whose S.M. Gordon & Associates firm supplies HP 3000 utilities, has worked with 3000s since 1983.


Copyright The 3000 NewsWire. All rights reserved.