View previous topic :: View next topic
|
Author |
Message |
stravi
New User
Joined: 04 Apr 2007 Posts: 2 Location: Chennai
|
|
|
|
We are getting lot of page-number codes from an online called program into the calling program. All these page code need to be sorted in this main CICS program. How can we do this? |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
There are a number of ways to sort within a cobol program, the question I have is why do they "need" to be sorted? |
|
Back to top |
|
|
stravi
New User
Joined: 04 Apr 2007 Posts: 2 Location: Chennai
|
|
|
|
Thanks for the reply....
we have number of ways in batch, but we need to sort in online (CICS) in which SORT can't be used extensively. Please suggest a way to sort.... |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
I knew I could find it.....
DavidatK wrote: |
Here's a site you may be interested in, when talking about sorts
Sorting Algorithms
Dave |
located in thread Cobol bubble sort which was the result of a simple search........ |
|
Back to top |
|
|
TG Murphy
Active User
Joined: 23 Mar 2007 Posts: 148 Location: Ottawa Canada
|
|
|
|
We have a COBOL copybook that contains heapsort logic. It performs a very efficient sort on a COBOL table. There are no external calls. We use it in both CICS and batch. Works well. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello TG,
Is it proprietary or might it be posted here? |
|
Back to top |
|
|
munikumar Currently Banned New User
Joined: 18 Apr 2007 Posts: 24 Location: India
|
|
|
|
We can use SORT well in on-line programs too. Performance matters only when the size of the internal table is very large.
Regards
Munikumar G |
|
Back to top |
|
|
TG Murphy
Active User
Joined: 23 Mar 2007 Posts: 148 Location: Ottawa Canada
|
|
|
|
Dick,
I'm sure the folks here won't mind me sharing this with you, given that we have greatly benefited from this site.
First a clarification: In my earlier post I simplified things by saying that this code was stored as a copybook. It is not. It is very similar to a copybook, but not really a copybook.
It is stored as a "frame" and our COBOL generator uses this frame as input and responds by generating COBOL code. Below I have included the generated COBOL code. I believe it possible to store this logic as 2 copybooks (one for Working Storage - one for code) and to make it generic so that it could handle any table. You would need to make clever use of the REPLACING when you COPY it in. I haven't tried this myself but I think you could make it work - that is create a generic set of copybooks that could be used by many programs to sort different tables.
Note: The program that contains this logic actually contains 2 instances of the sort logic - the one shown in this post sorts by NAME - the other instance sorts by SEASON and is not shown in this post. Any attempt to repackage this logic in a copybook should keep this in mind (ie - the ability to copy in the logic multiple times without getting duplicate compile errors)
I am not the author of this sort logic but I can tell you we use it in many production programs - both batch and CICS.
Table that gets sorted
Code: |
01 WS-WORK-FIELDS.
05 WS-SUB PIC S9(4) COMP.
05 WS-MONTH-TABLE.
10 WS-MONTH-TABLE-MAXSIZE PIC S9(4) COMP VALUE +12.
10 WS-MONTH-VALUES.
15 WS-JANUARY PIC X(10) VALUE "January".
15 WS-JANUARY-SQNBR PIC 9(02) VALUE 1.
15 WS-JANUARY-SEASON PIC X(7) VALUE "Winter".
15 WS-FEBRUARY PIC X(10) VALUE "February".
15 WS-FEBRUARY-SQNBR PIC 9(2) VALUE 2.
15 WS-FEBRUARY-SEASON PIC X(7) VALUE "Winter".
15 WS-MARCH PIC X(10) VALUE "March".
15 WS-MARCH-SQNBR PIC 9(2) VALUE 3.
15 WS-MARCH-SEASON PIC X(7) VALUE "Winter".
15 WS-APRIL PIC X(10) VALUE "April".
15 WS-APRIL-SQNBR PIC 9(2) VALUE 4.
15 WS-APRIL-SEASON PIC X(7) VALUE "Spring".
15 WS-MAY PIC X(10) VALUE "May".
15 WS-MAY-SQNBR PIC 9(2) VALUE 5.
15 WS-MAY-SEASON PIC X(7) VALUE "Spring".
15 WS-JUNE PIC X(10) VALUE "June".
15 WS-JUNE-SQNBR PIC 9(2) VALUE 6.
15 WS-JUNE-SEASON PIC X(7) VALUE "Summer".
15 WS-JULY PIC X(10) VALUE "July".
15 WS-JULY-SQNBR PIC 9(2) VALUE 7.
15 WS-JULY-SEASON PIC X(7) VALUE "Summer".
15 WS-AUGUST PIC X(10) VALUE "August".
15 WS-AUGUST-SQNBR PIC 9(2) VALUE 8.
15 WS-AUGUST-SEASON PIC X(7) VALUE "Summer".
15 WS-SEPTEMBER PIC X(10) VALUE "September".
15 WS-SEPTEMBER-SQNBR PIC 9(2) VALUE 9.
15 WS-SEPTEMBER-SEASON PIC X(7) VALUE "Autumn".
15 WS-OCTOBER PIC X(10) VALUE "October".
15 WS-OCTOBER-SQNBR PIC 9(2) VALUE 10.
15 WS-OCTOBER-SEASON PIC X(7) VALUE "Autumn".
15 WS-NOVEMBER PIC X(10) VALUE "November".
15 WS-NOVEMBER-SQNBR PIC 9(2) VALUE 11.
15 WS-NOVEMBER-SEASON PIC X(7) VALUE "Autumn".
15 WS-DECEMBER PIC X(10) VALUE "December".
15 WS-DECEMBER-SQNBR PIC 9(2) VALUE 12.
15 WS-DECEMBER-SEASON PIC X(7) VALUE "Winter".
10 WS-MONTH-ENTRY REDEFINES WS-MONTH-VALUES
OCCURS 12 TIMES.
15 WS-MONTH-NM PIC X(10).
15 WS-MONTH-SQNBR PIC 9(2).
15 WS-SEASON-NM PIC X(7). |
Working storage fields used by the sort logic.
Code: |
01 SORT-BY-NAME-VARIABLES.
05 SORT-BY-NAME-ORDER PIC X VALUE "A".
05 SORT-BY-NAME-OK PIC X.
05 SORT-BY-NAME-IDX PIC S9(9) COMP VALUE ZERO.
05 SORT-BY-NAME-IDX2 PIC S9(9) COMP VALUE ZERO.
05 SORT-BY-NAME-GAP PIC S9(9) COMP VALUE ZERO.
05 SORT-BY-NAME-TABLE-SIZE PIC S9(9) COMP VALUE ZERO.
05 SORT-BY-NAME-LOOP PIC S9(9) COMP VALUE ZERO.
05 SORT-BY-NAME-TEMP PIC X(1000). |
To sort the table, I would code a PERFORM SORT-BY-NAME-MAIN in my logic. Note that the code that follows is not coded by me - rather it is generated by our COBOL generator. The only thing I code is the PERFORM and I use proprietary commands (equivilant to the COBOL COPY command) to copy in the generic sort logic.
Code: |
SORT-BY-NAME-MAIN SECTION.
SORT-BY-NAME-MAIN-01.
MOVE WS-MONTH-TABLE-MAXSIZE TO SORT-BY-NAME-TABLE-SIZE.
* Gap divides the table into parts. The gap will get increasingly
* smaller as the table is sorted.
*
DIVIDE 2 INTO SORT-BY-NAME-TABLE-SIZE GIVING
SORT-BY-NAME-GAP.
*
* Perform the outer loop until the gap reaches zero.
*
PERFORM SORT-BY-NAME-PASS-LOOP UNTIL SORT-BY-NAME-GAP = 0.
SORT-BY-NAME-PASS-LOOP SECTION.
SORT-BY-NAME-PASS-LOOP-01.
PERFORM SORT-BY-NAME-PASS-TABLE
VARYING SORT-BY-NAME-LOOP FROM 1 BY 1
UNTIL SORT-BY-NAME-LOOP =
SORT-BY-NAME-TABLE-SIZE - SORT-BY-NAME-GAP + 1.
DIVIDE 2 INTO SORT-BY-NAME-GAP.
SORT-BY-NAME-PASS-TABLE SECTION.
*
* Test items starting with SORT-BY-NAME-LOOP.
* The distance between keys being compared is SORT-BY-NAME-GAP.
* If when a set of keys are found out of order, the rows
* are swaped AND the index is reduced by 'gap'. This
* causes the compares to back up to see if the key just
* moved up, might need to be moved up again. Essentially,
* causing the row to 'bubble-up' quickly and optimizes
* for rows that are very much out of order.
*
SORT-BY-NAME-PASS-TABLE-01.
MOVE SORT-BY-NAME-LOOP TO SORT-BY-NAME-IDX.
PERFORM SORT-BY-NAME-COMPARISONS
UNTIL SORT-BY-NAME-IDX < 0 OR SORT-BY-NAME-IDX = 0.
SORT-BY-NAME-COMPARISONS SECTION.
SORT-BY-NAME-COMPARISONS-01.
ADD SORT-BY-NAME-IDX SORT-BY-NAME-GAP GIVING
SORT-BY-NAME-IDX2
PERFORM SORT-BY-NAME-COMPARE-KEYS
IF SORT-BY-NAME-OK = "N"
PERFORM SORT-BY-NAME-SWAP-ROWS
SUBTRACT SORT-BY-NAME-GAP FROM SORT-BY-NAME-IDX
ELSE
MOVE 0 TO SORT-BY-NAME-IDX.
SORT-BY-NAME-COMPARE-KEYS SECTION.
*
* Compare keys here.
* This routine returns SORT-BY-NAME-OK = N if the comparison is tr
* This means that the keys being compared are out of order and
* must be switched.
*
SORT-BY-NAME-COMPARE-KEY-01.
MOVE "Y" TO SORT-BY-NAME-OK.
IF ( SORT-BY-NAME-ORDER = "A" AND
WS-MONTH-NM(SORT-BY-NAME-IDX) >
WS-MONTH-NM(SORT-BY-NAME-IDX2) )
OR ( SORT-BY-NAME-ORDER = "D" AND
WS-MONTH-NM(SORT-BY-NAME-IDX) <
WS-MONTH-NM(SORT-BY-NAME-IDX2) )
MOVE "N" TO SORT-BY-NAME-OK.
SORT-BY-NAME-SWAP-ROWS SECTION.
SORT-BY-NAME-SWAP-ROWS-01.
MOVE WS-MONTH-ENTRY(SORT-BY-NAME-IDX)
TO SORT-BY-NAME-TEMP (1:LENGTH OF WS-MONTH-ENTRY(1))
MOVE WS-MONTH-ENTRY(SORT-BY-NAME-IDX2) TO
WS-MONTH-ENTRY(SORT-BY-NAME-IDX)
MOVE SORT-BY-NAME-TEMP (1: LENGTH OF WS-MONTH-ENTRY(1))
TO WS-MONTH-ENTRY(SORT-BY-NAME-IDX2). |
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hi TG,
Thank you for posting the code |
|
Back to top |
|
|
Raphael Bacay
New User
Joined: 04 May 2007 Posts: 58 Location: Manila, Philippines
|
|
|
|
If you can do it in batch maybe you can still do it in batch. You can keep a file to store your page-numbers. In your CICS program you can manually close disable the file. Write the JCL to sort in Working Storage and use Online Submitted Job technique from CICS to execute the SORT JCL written in working storage. You can then open-enable the the file in cics once the job has finished and access the now sorted file.
Hope this helps. |
|
Back to top |
|
|
|