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

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: