Search code examples
ibm-midrangerpgle

How to process a SUBFILE using For Loop with a CHAIN rather than using READC and SFLNXTCHG


How to process a SUBFILE using For Loop with a CHAIN rather than using READC and SFLNXTCHG. Readc along with the Sflnxtchg option sometimes becomes a bit complex to understand , I have heard that instead of that a forloop and chain combination can be used to achieve the same result, Can some one prove a code snipped along with a bit of explanation for that.


Solution

  • A simple example of the declarations and loop you would use would be:

    FMYDSP     CF   E             WORKSTN SFILE(SFLREC01:sflIndex)                          
    D sflIndex        S              4S 0
    D sflMax          S              4S 0
    
      FOR sflIndex = 1 TO sflMax; 
        CHAIN sflIndex SFLREC01;   
     // do stuff
    
      ENDFOR;
    

    I prefer to use FOR for reading subfiles over a DOW or DOU loop. One reason for my preference is because when using do loops to read, anytime there is a "last record" condition, the system writes an annoying message to the job log. For example, you might think that the following is a textbook use of DOW:

    CHAIN(E) 1 SFLREC01;                    
    DOW %found;                             
       // do stuff
    
      sflIndex += 1;                                 
      CHAIN(E) sflIndex SFLREC01;                    
    ENDDO;                                           
    

    but even if you try to use the (E) extension to prevent it, you will still get a severity 30 message in the job log:

    Message ID . . . . . . : CPF5020 Severity . . . . . . . : 30
    Message type . . . . . : Notify
    Date sent . . . . . . : 01/20/22 Time sent . . . . . . : 10:11:16
    Message . . . . : Subfile record not found.
    Cause . . . . . : The input operation to the subfile specified a relative record number for which no active subfile record exists.
    Recovery . . . : See the Application Display Programming book, SC41-5715, for subfile processing.
    Possible choices for replying to message . . . . . . . . . . . . . . . :
    I -- Request is ignored. Control is returned to user.
    C -- Request is canceled. Escape msg CPF5104 is sent.

    The system sends the cancel reply for you, you don't have to do anything, but it is annoying that the log fills up with these and obscures the record of what the program is actually doing. You end up with bunch of these messages, often one for every time the user presses enter. This alone was enough to make me switch to FOR loops and to keep a variable around to remember my total number of entries.

    enter image description here