Page 1 of 1

Reversal of records

PostPosted: Thu Oct 25, 2012 12:13 am
by VIJAY1
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?

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 12:17 am
by Robert Sample
     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.

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 12:36 am
by Pandora-Box
Also its better if you Compute before read

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 12:50 am
by dick scherrer
Hello,

Why do you believe this?

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 1:13 am
by Pandora-Box
Because that will ensure Ws-I has value 4.And not 5

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 1:26 am
by dick scherrer
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.

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 2:03 am
by BillyBoyo
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.

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 10:26 am
by Pandora-Box
Yes I was able to figure it out :-) Thanks Bill & Dick

Re: Reversal of records

PostPosted: Thu Oct 25, 2012 12:48 pm
by VIJAY1
Thanks all.i got it now..