Reading a File Twice with CL

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Have you ever wished you could read a file more than once within a CL program? Writing two programs to accomplish multiple passes of a file is unnecessary. The key to the solution is to use the Transfer Control (TFRCTL) command.

The TFRCTL and CALL commands are similar. If you execute a CALL command in a CL program, control returns to the command following the CALL after the called program completes. TFRCTL is the same as CALL, except that the invoking program does not regain control after the called program completes. A CL program may use TFRCTL to invoke itself without a recursion error. 6 contains an example of a CL program that uses TFRCTL to read through a data file twice: once to accumulate a total cost and once to calculate the percent of total cost for each record in the file.

The TFRCTL and CALL commands are similar. If you execute a CALL command in a CL program, control returns to the command following the CALL after the called program completes. TFRCTL is the same as CALL, except that the invoking program does not regain control after the called program completes. A CL program may use TFRCTL to invoke itself without a recursion error. Figure 6 contains an example of a CL program that uses TFRCTL to read through a data file twice: once to accumulate a total cost and once to calculate the percent of total cost for each record in the file.

You can invoke the program in 6, THISCLPGM, from any AS/400 command line by entering the CALL THISCLPGM PARM('1' 0) command. The second parameter in the program, *DEC (15 5), is the only type of numeric parameter that you can pass from the command line. Because THISCLPGM receives a value of '1' in the first parameter (&PASS) on the first pass, it executes the code starting at tag LOOP1. The program reads through the file to produce the portfolio total. It uses the variable &TOTAL to accumulate this amount. When the program reaches end of file, it invokes itself through the TFRCTL command. See the first Monitor Message (MONMSG) statement. This time, the &PASS parameter contains the value '2' to signify the second pass. It also passes the total sales (&TOTAL) as the second parameter.

You can invoke the program in Figure 6, THISCLPGM, from any AS/400 command line by entering the CALL THISCLPGM PARM('1' 0) command. The second parameter in the program, *DEC (15 5), is the only type of numeric parameter that you can pass from the command line. Because THISCLPGM receives a value of '1' in the first parameter (&PASS) on the first pass, it executes the code starting at tag LOOP1. The program reads through the file to produce the portfolio total. It uses the variable &TOTAL to accumulate this amount. When the program reaches end of file, it invokes itself through the TFRCTL command. See the first Monitor Message (MONMSG) statement. This time, the &PASS parameter contains the value '2' to signify the second pass. It also passes the total sales (&TOTAL) as the second parameter.

Since the value in &PASS is '2', the program executes the code starting at tag LOOP2. This time, while reading through the SALES file, it calculates a percentage of sales amount using the &TOTAL accumulated in the original call of the program. This calculation, done with the Change Variable (CHGVAR) command, is necessary because the program that this program calls (UPDSUMRY) requires this percentage value as a passed parameter.

-Mike Cravitz


Reading a File Twice with CL

Figure 6 Reading a File Twice with a CL Program

 THISCLPGM: PGM PARM(&PASS &TOTAL) DCL VAR(&PASS) TYPE(*CHAR) LEN(1) DCL VAR(&TOTAL) TYPE(*DEC) LEN(15 5) DCL VAR(&PERCENT) TYPE(*DEC) LEN(5 2) DCLF FILE(SALES) IF COND(&PASS *EQ '1') THEN(DO) CHGVAR VAR(&PASS) VALUE('2') CHGVAR VAR(&TOTAL) VALUE(0) LOOP1: RCVF MONMSG MSGID(CPF0864) EXEC(TFRCTL PGM(THISCLPGM) + PARM(&PASS &TOTAL)) CHGVAR VAR(&TOTAL) VALUE(&TOTAL + &COST) GOTO CMDLBL(LOOP1) ENDDO LOOP2: RCVF MONMSG MSGID(CPF0864) EXEC(GOTO CMDLBL(ENDPGM)) CHGVAR VAR(&PERCENT) VALUE((&COST /&TOTAL) * 100) CALL PGM(UPDSUMRY) PARM(&ITEMID &PERCENT) GOTO CMDLBL(LOOP2) ENDPGM: ENDPGM 
BLOG COMMENTS POWERED BY DISQUS