In an effort to hasten the completion of projects, most programmers "steal" code from other sources within their libraries. The problem is that many times, the code originated years ago and is cumbersome, lengthy, and not easy to maintain. Even most ILE code resulted from old code that was passed through a conversion utility. Using the functionality of RPGIV's Keywords and Built-In-Functions usually can make the code much smaller and manageable. Here are just a couple of examples that I've come upon recently.
Example 1:
A file contains many fields used as buckets, such as monthly totals, and each field needs to be interrogated.
OLD WAY:
Define a Data Structure with each file field as a subfield and define an array and move data structure to array.
FINPUTFIL IF E K DISK D ARY S 10 DIM(4) D FILDS DS 40 D FLD01 1 10 D FLD02 11 20 D FLD03 21 30 D FLD04 31 40 C FILKEY CHAIN INPUTFIL
.... Move each Field to array and do processing
or
Do a rename of each field.
IINPFIL I FLD01 ARY(1) I FLD02 ARY(2) I FLD03 ARY(3) I FLD04 ARY(4)
NEW WAY:
Define External Data Structure and Overlay Array on Data Structure
FINPUTFIL IF E K DISK D FILDS E DS 40 EXTNAME(INPUTFIL) D ARY 1 40 DIM(4) C FILKEY CHAIN INPUTFIL .... Now ARY(1) = FLD01, etc. automatically
Example 2:
Date Validation.
OLD WAY:
Use Data Structures and moves to check each field. This example was passed on to many different programs.
I DS I 1 60DSPDAT I 1 20$@M I 3 40$@D I 5 60$@Y C $@D CABLT1 $TG001 11 C $@M CABGT12 $TG001 11 C $@M CABLT1 $TG001 11 C $@Y DIV 4 $@REM 10 C MVR $@REM C Z-ADD$@M $@MS 20 C $@REM IFEQ 0 C $@M IFEQ 2 C Z-ADD13 $@MS C END C END C $@D CABGT$@E,$@MS $TG001 11
NEW WAY:
Use ILE Date operations and edits.
D Today S D Inz(*SYS) D WrkDate S D D Days S 4 0 C *MDY Test(DE) DSPDAT C If %Error C Eval *In81 = *On C Endif C *MDY Move DSPDAT WrkDate C WrkDate SubDur Today Days:*D C If Days < *Zero C Eval *In81 = *On C Endif
Example 3:
Building text strings.
OLD WAY:
The most common examples include using arrays with loops that test for each character or builds an array of characters and moves the array to a text field.
NEW WAY:
Using BIFs like %Editc, %Subst, %Scan, %Trim, %Xlate will allow complex text strings to be validated and built with as little as one eval statement through concatenation.
DExcCmd PR ExtPgm('QCMDEXC')
D Command 320 Const
D Length 15 5 Const
C Eval Clock# = %Subst(User:3:5)
C Eval #UsrDta = %Subst(Pm1CLASS:1:2) + '-' +
C %Trim(%Editc(JobTckt#:'Z'))
C Eval #SplNm = %Subst(DayOfWeek:1:3) +
C PullHouse
C Eval Command = 'OVRPRTF BCLABELS OUTQ(' + Outq +
C ') HOLD(*YES) SAVE(*NO) SPLFNAME(' +
C #SplNm + ') USRDTA(''' + #UsrDta + ''')'
C Eval WrkClass = Pm1CLASS
C Callp ExcCmd(Command:Length)
Moral of the story: Use the full capabilities of RPGIV with Keywords and Bifs, especially when taking old code and re-using it.
================================== MORE INFORMATION ON THIS TOPIC ==================================
The Best Web Links: tips, tutorials and more.
Ask your systems management questions--or help out your peers by answering them--in our live discussion forums.
This was first published in March 2002