Search code examples
filerpglerpg

Replacing Primary file in RPGLE


Does anyone has experience in replacing the primary file from a RPG program, which has Level indicators defined (Level indicator used for calculation purpose)?

Instead of RPG cycle , i need to introduce new file (replacing primary file) which will defined as a fully procedural file (no more primary file)..

Thanks.


Solution

  • Why do you want to stop using a primary file? Doing your own comparisons to simulate level indicators will probably be much more work than making the changes to the primary file.

    In general, to replace a primary file with a full-procedural file, put the READ + DOW not %EOF(primaryfile) at the top of the calculations, and put the second READ and ENDDO at the end of calculations.

    For the calculations with the level indicators L0, L1 etc in columns 7 and 8, I would first move those calculations into subroutines, for now call them something like L0_subr, L1_subr etc.

    To handle the comparisons with previous record, after the first READ, before the DOW, add statements to save off the current values of the level-indicator fields. At the end of calculations, before the second READ, add statements to compare the current values with the saved values, and if they are different, call the relevant Lx_subr(s). After all the Lx_subrs have been called, update the saved values for that particular level indicator.

    Making that kind of change is error prone. I would just leave it as a primary file and add extra Level indicators to the I specs, if necessary.

    2017-11-06 update starting from here:

    To enable keeping track of both the previous record and the current record, use the feature where you can read into a data structure.

    read rec cur_ds; 
    dow not %eof;
        ... 
        if have_prv_ds; 
          compare the previous record to the current record 
        endif; 
        eval-corr prv_ds = cur_ds; 
        have_prv_ds = *on; 
        read rec cur_ds; 
    enddo; 
    

    Since the READ won't be affecting the standalone fields associated with the file, a good practice to avoid accidentally referring to those standalone fields is to avoid even having the standalone fields defined. To do that, define the file with the QUALIFIED keyword. Then, you'd refer to the record format using file.fmt, and the fields associated with the file wouldn't exist.

    dcl-f myfile qualified;
    dcl-ds cur_ds likerec(myfile.fmt);
    dcl-ds prv_ds likerec(myfile.fmt);
    
    read myfile.fmt cur_ds; 
    dow not %eof(myfile);
        ... 
        if have_prv_ds; 
          compare the previous record to the current record 
        endif; 
        eval-corr prv_ds = cur_ds; 
        have_prv_ds = *on; 
        read myfile.fmt cur_ds; 
    enddo;