Reversal of records



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Reversal of records

Postby VIJAY1 » Thu Oct 25, 2012 12:13 am

This is the code which i wrote to reverse the order of unsorted records and copy into another file.
Just for simplicity, i gave only 4 records.
IDENTIFICATION DIVISION.               
PROGRAM-ID. REV.                       
ENVIRONMENT DIVISION.                   
INPUT-OUTPUT SECTION.                   
FILE-CONTROL.                           
    SELECT MYFILE1 ASSIGN TO MYCOB1     
    FILE STATUS IS WS-STAT.             
    SELECT MYFILE2 ASSIGN TO MYCOB2.   
DATA DIVISION.                         
FILE SECTION.                           
FD MYFILE1                             
    RECORDING MODE IS F.               
01 REC1 PIC X(80).                     
FD MYFILE2
     RECORDING MODE IS F.                   
 01 REC2 PIC X(80) VALUE ZEROES.     
 WORKING-STORAGE SECTION.           
 01 WS-VAR1.                         
 10 VAR1 PIC X(80) OCCURS 4 TIMES.   
 01 WS-I PIC 9(2) VALUE 1.           
 01 WS-STAT PIC 9(02).               
 PROCEDURE DIVISION.                 
     OPEN INPUT MYFILE1.             
     OPEN OUTPUT MYFILE2.           
     PERFORM UNTIL WS-STAT = 10     
     READ MYFILE1 INTO VAR1(WS-I)   
     COMPUTE WS-I = WS-I + 1         
     END-PERFORM.                   
     PERFORM UNTIL WS-I = 0         
     WRITE REC2 FROM VAR1(WS-I)     
     COMPUTE WS-I = (WS-I) - 1   
     END-PERFORM.                 
     STOP RUN.   

When i run this code,I get the output file as first record is 04 10 ..and then the reversed records.
i cant understand why.
can someone help?
VIJAY1
 
Posts: 26
Joined: Sun Oct 21, 2012 2:49 pm
Has thanked: 3 times
Been thanked: 0 time

Re: Reversal of records

Postby Robert Sample » Thu Oct 25, 2012 12:17 am

     COMPUTE WS-I = WS-I + 1         
     END-PERFORM. 
At this point, WS-I is set to 5 if you read 4 records.  Writing the 5th element of a table with 4 elements will give you the results you saw.

These users thanked the author Robert Sample for the post:
VIJAY1 (Thu Oct 25, 2012 12:21 am)
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Reversal of records

Postby Pandora-Box » Thu Oct 25, 2012 12:36 am

Also its better if you Compute before read
User avatar
Pandora-Box
 
Posts: 65
Joined: Fri Feb 10, 2012 8:30 pm
Location: Mars
Has thanked: 3 times
Been thanked: 6 times

Re: Reversal of records

Postby dick scherrer » Thu Oct 25, 2012 12:50 am

Hello,

Why do you believe this?
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Reversal of records

Postby Pandora-Box » Thu Oct 25, 2012 1:13 am

Because that will ensure Ws-I has value 4.And not 5
User avatar
Pandora-Box
 
Posts: 65
Joined: Fri Feb 10, 2012 8:30 pm
Location: Mars
Has thanked: 3 times
Been thanked: 6 times

Re: Reversal of records

Postby dick scherrer » Thu Oct 25, 2012 1:26 am

Hello,

Which is true because the value was started at 1 rather than zero. Methinks it is better to start with zero and then add 1 when a record is actually read so the count reflects what has happened rather than what will happen.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Reversal of records

Postby BillyBoyo » Thu Oct 25, 2012 2:03 am

Dick is correct. As it stands, if the file contains no records, it will attempt to write one record, because of the initial value of one for the count. Always ensure that counts, and the things they are counting, are in step.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Reversal of records

Postby Pandora-Box » Thu Oct 25, 2012 10:26 am

Yes I was able to figure it out :-) Thanks Bill & Dick
User avatar
Pandora-Box
 
Posts: 65
Joined: Fri Feb 10, 2012 8:30 pm
Location: Mars
Has thanked: 3 times
Been thanked: 6 times

Re: Reversal of records

Postby VIJAY1 » Thu Oct 25, 2012 12:48 pm

Thanks all.i got it now..
VIJAY1
 
Posts: 26
Joined: Sun Oct 21, 2012 2:49 pm
Has thanked: 3 times
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post