IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

doubt of SORT in COBOL program


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
i413678
Currently Banned

Active User


Joined: 19 Feb 2005
Posts: 112
Location: chennai

PostPosted: Fri Mar 03, 2006 12:20 pm
Reply with quote

Hi,

I have a input file with 100 records.

I want the output file with below requirement:

1.first 20 records has to be copied as it is to the output file
2.next 21 to 40 records I want to copy in ascending order to output file
3.next 41 to 60 records I have to copy as it is to output file.
4.next 61 to 80 records I want to copy in descending orderto output file.
5.next remaining records i.e.81 to 100 records I want to copy as it is to the output file.

I hope you understood the problem.....this I want to do in COBOL program only.....

May I know how it can be done........

thx in advance....................

pavan
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Mon Mar 06, 2006 4:58 pm
Reply with quote

Pavan,

Here?s some code that will do what your requirements are. This code works, its been tested.

This code is not optimum, does not use the COBOL SORT statement, and is not up to good coding standards, and is written for your very specific requirements, but I hope it will give you some ideas about how to construct you program.

Note about my personal feelings on the COBOL internal SORT. Don?t us it unless you absolutely have to; it?s very inefficient. When ever I have to do internal sorts, and it shouldn?t be very often, if the all the sort records can be held in a COBOL table, I use a bubble sort to sort the records. If there is a situation where I have to do one massive sort, I break the process into two programs and use an external sort.

But, that?s off the subject. In the program below, there are examples of two versions of a bubble sort, one for asc, and one for desc.

Have fun,


Code:

FILE-CONTROL.                                     
                                                   
    SELECT FILE-IN                                 
        ASSIGN TO FILEIN.                         
                                                   
    SELECT FILE-OUT                               
        ASSIGN TO FILEOUT.                         
                                                   
                                                   
DATA DIVISION.   
FILE SECTION.                                               
                                                           
FD  FILE-IN                                                 
    RECORDING MODE IS F                                     
    LABEL RECORD IS STANDARD                               
    BLOCK CONTAINS 0 RECORDS.                               
                                                           
01  FILE-REC-IN                  PIC X(80).                 
                                                           
FD  FILE-OUT                                               
    RECORDING MODE IS F                                     
    LABEL RECORD IS STANDARD                               
    BLOCK CONTAINS 0 RECORDS.                               
                                                           
01  FILE-REC-OUT                PIC X(80).                 
                                                           
WORKING-STORAGE SECTION.                                       
                                                               
 01  FILE-RECORD                 PIC X(80).                     
 01  FILE-RECORD-ARRAY.                                         
     05  RECORD-HOLD             PIC X(80)      OCCURS 20 TIMES.
                                                               
 01  SUB1                        PIC S9(3)      COMP-3.         
 01  SUB2                        PIC S9(3)      COMP-3.         
 01  SORT-SUB1                   PIC S9(3)      COMP-3.         
 01  SORT-SUB2                   PIC S9(3)      COMP-3.         
 01  SWAP-COUNT                  PIC S9(3)      COMP-3         
                                                VALUE 0.       

PROCEDURE DIVISION.                           
                                               
    OPEN INPUT FILE-IN                         
         OUTPUT FILE-OUT.                     
                                               
    PERFORM                                                     
      VARYING SUB1 FROM 1 BY 1                                 
      UNTIL SUB1 > 5                                           
        PERFORM                                                 
          VARYING SUB2 FROM 1 BY 1                             
          UNTIL SUB2 > 20                                       
            PERFORM READ-INPUT                                 
            MOVE FILE-RECORD    TO RECORD-HOLD(SUB2)           
        END-PERFORM                                             
        EVALUATE SUB1                                           
          WHEN 2                                               
            PERFORM SORT-ASC                                   
          WHEN 4                                               
            PERFORM SORT-DESC                                   
        END-EVALUATE                                           
        PERFORM                                                 
          VARYING SUB2 FROM 1 BY 1                             
          UNTIL SUB2 > 20                                       
            MOVE RECORD-HOLD(SUB2) TO FILE-RECORD               
           PERFORM WRITE-OUTPUT                   
       END-PERFORM                               
   END-PERFORM.                                   
                                                 
   CLOSE FILE-IN                                 
         FILE-OUT.                               
                                                 
   GOBACK.                                       
SORT-ASC.                                                       
                                                                 
    PERFORM                                                     
      VARYING SORT-SUB1 FROM 1 BY 1                             
      UNTIL SORT-SUB1 > 19                                       
        COMPUTE SORT-SUB2 = SORT-SUB1 + 1                       
        PERFORM                                                 
          VARYING SORT-SUB2 FROM SORT-SUB2 BY 1                 
          UNTIL SORT-SUB2 > 20                                   
            IF RECORD-HOLD(SORT-SUB1) > RECORD-HOLD(SORT-SUB2)   
            THEN                                                 
                MOVE RECORD-HOLD(SORT-SUB1)                     
                                    TO FILE-RECORD               
                MOVE RECORD-HOLD(SORT-SUB2)                     
                                    TO RECORD-HOLD(SORT-SUB1)   
                MOVE FILE-RECORD    TO RECORD-HOLD(SORT-SUB2)   
            END-IF                                               
        END-PERFORM                                             
    END-PERFORM.                                                 
SORT-DESC.                                                           
                                                                     
    PERFORM WITH TEST AFTER                                         
      UNTIL SWAP-COUNT = 0                                           
        MOVE 0                    TO SWAP-COUNT                     
        PERFORM                                                     
          VARYING SORT-SUB1 FROM 1 BY 1                             
          UNTIL SORT-SUB1 > 19                                       
          IF RECORD-HOLD(SORT-SUB1 + 1) > RECORD-HOLD(SORT-SUB1)     
          THEN                                                       
              MOVE RECORD-HOLD(SORT-SUB1 + 1)                       
                                  TO FILE-RECORD                     
              MOVE RECORD-HOLD(SORT-SUB1)                           
                                  TO RECORD-HOLD(SORT-SUB1 + 1)     
              MOVE FILE-RECORD    TO RECORD-HOLD(SORT-SUB1)         
              ADD +1              TO SWAP-COUNT                     
          END-IF                                                     
        END-PERFORM                                                 
    END-PERFORM.                                                     
READ-INPUT.                                         
    READ FILE-IN              INTO FILE-RECORD.     
WRITE-OUTPUT.                                       
    WRITE FILE-REC-OUT        FROM FILE-RECORD.


Dave
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Need to set RC4 through JCL SORT DFSORT/ICETOOL 5
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Using API Gateway from CICS program CICS 0
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
Search our Forums:

Back to Top