Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Please explain me this two READ for the same file

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
sh_moni
Currently Banned

New User


Joined: 30 Apr 2007
Posts: 7
Location: pune

PostPosted: Mon Jun 25, 2007 7:39 pm    Post subject: Please explain me this two READ for the same file
Reply with quote

Code:

READ BOOKSALESFILE                   
    AT END SET ENDOFBSF TO TRUE       
END-READ                             
PERFORM UNTIL ENDOFBSF               
   IF NORMALSALE                     
    RELEASE WORKREC FROM BSF-RECORD   
   END-IF                             
   READ BOOKSALESFILE                 
   AT END SET ENDOFBSF TO TRUE       
   END-READ                           
END-PERFORM                   

Could somebody please explain me this part of code,why we are using here two READ for the same file,if i remove the first one the output file is empty one and if i remove the internal read the program goes in infinite loop.
Please help me out.
Back to top
View user's profile Send private message

TG Murphy

Active User


Joined: 23 Mar 2007
Posts: 149
Location: Ottawa Canada

PostPosted: Mon Jun 25, 2007 8:36 pm    Post subject:
Reply with quote

sh_moni,

It is very common to see 2 READS as shown in your email. In fact that is the way I code it myself. Reason why is that the PERFORM UNTIL is actually a DOWHILE construct - not a DOUNTIL as the COBOL syntax would suggest. The condition gets tested first and then the logic within the loop only executes if the condition is true.

The alternative coding that would let you code just a single READ would look something like this:

PERFORM WITH TEST AFTER UNTIL EOF
PERFORM READ
IF NOT EOF
PERFORM PROCESS-RECORD
END-IF
END-PERFORM

And this is also a perfectly fine way to do it.

However, there are few common types of programs where you need special logic that executes ONLY for the first record that you read. (ie control break logic) For these programs, the "2 READ" approach is perfectly suited to doing this. So there is a good reason why you bump into the "2 READ" approach so often.
Back to top
View user's profile Send private message
ramfrom84

New User


Joined: 23 Aug 2006
Posts: 93
Location: chennai

PostPosted: Mon Jun 25, 2007 8:37 pm    Post subject:
Reply with quote

Hi,
Mostly READ can be used for TWO times , only if we want the prevoius record value for comparison or rolling up the records , then they will read one time at the begining , for next time onwards u can read and do the process with operation(having previous value).

It is suggestion since u have mention only few code, u can trace the program for better understanding.
Back to top
View user's profile Send private message
socker_dad

Active User


Joined: 05 Dec 2006
Posts: 147
Location: Columbia, MO

PostPosted: Tue Jun 26, 2007 1:46 am    Post subject: Re: Please explain me this two READ for the same file
Reply with quote

To put it in ancient terminology (cause I am an old fart and read and write this kinf od code all day!), this is what your code does.

The first read is called an initial read. It reads the file and loads the first record for subsequent processing. If there is no first record, the end of file condition is set, in your case, ENDOFBSF. The END-READ closes this statement.

The PERFORM UNTIL is a separate piece of logic. It basically states that we are going to test the NORMALSALE condition and Release the Workrec from the BSF-RECORD if it is true. Then we read the next record in the Book Sale File, setting ENDOFBSF to true when we reach the end of the file.

Once the condition ENDOFBSF is true, we drop out of the PERFORM UNTIL statement. This could happen on the first record or on the ninety-first record.

The idea is that you use the first Read once to open the file and get the first record, perform processing on the record, then read the next record. The UNTIL logic keeps your program from attempting to perform operations on records that do not exist. And the Read at the bottom of the Perform logic allows you to read the next record and set an end of file condition before attempting to do any work on the record.

As previously mentioned, this is very, very common logic and if you work a lot with batch programs, you will find many of them use this basic logic.

Hope that was clear.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3158
Location: Tucson AZ

PostPosted: Tue Jun 26, 2007 3:43 am    Post subject:
Reply with quote

To add to Dad's post.....

When you program, there are three parts of the logic. The main and primary is the central logic - the read/write sort of thing. Before and after that are the "boundary conditions", how to start and how to finish....the initial read set up the program so that it could loop through the central section seamlessly.

Personally, when I code such logic, I always start with the loop and then figure out how to start and stop it....

Good luck.
Back to top
View user's profile Send private message
sh_moni
Currently Banned

New User


Joined: 30 Apr 2007
Posts: 7
Location: pune

PostPosted: Tue Jun 26, 2007 2:00 pm    Post subject: Re: Please explain me this two READ for the same file
Reply with quote

hi all,

many thanks for your explanations,its quite clear to me know.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Changing of LRECL of a file abdulrafi DFSORT/ICETOOL 2 Fri Mar 24, 2017 3:25 pm
No new posts splitting a file abdulrafi DFSORT/ICETOOL 4 Fri Mar 24, 2017 11:51 am
No new posts Receive a file using PCOMM macro Harald.v.K IBM Tools 0 Thu Mar 23, 2017 6:50 pm
No new posts Export flat file data into excel sheet murali.andaluri DFSORT/ICETOOL 2 Mon Mar 20, 2017 5:39 pm
No new posts Formatting VB File Learncoholic DFSORT/ICETOOL 3 Mon Mar 20, 2017 12:29 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us