Use CAT Instead of MOVEL
The RPG operation code MOVEL is restricted to moving eight characters in factor 2 to the Result field. Use the CAT operation code to move left up to 16 characters in one operation, as in:
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 C '12345678'CAT '90ABCDEF'RESULT 16
Saving and Restoring Indicators
By making use of the RPG/400 predefined *IN array, you can save up to 99 indicators for those times when you don't have any indicators left or you don't want to be bothered finding an unused indicator. Here's an example of saving selected contiguous indicators 61-68.
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 C MOVEA*IN,61 SAV8 8 C EXSR SUB1 C MOVEASAV8 *IN,61
The idea here is that all of the numeric indicators, 01-99, are mapped into the *IN array. The settings of a contiguous group of indicators can be saved by moving the *IN array to a save field, then restoring the save field into the *IN array. Using the same technique, you can also save all 99 indicators, simply by using a larger save field and applying the MOVEA to the entire *IN array, as illustrated here.
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 C MOVEA*IN SAV99 99 C EXSR SUB1 C MOVEASAV99 *IN
This technique can also be used for nested subroutines, if you use a separate save field for each subroutine.
Reading Multiple Subfile Records
One little-known fact about RPG is that it can process several READ operation codes simultaneously. It's easy to take advantage of this feature.
Suppose you name your subfile control record SFLCTL. Above it, there's an input-capable record named ABOVE, and below it is another called BELOW.
Code your RPG program as shown below. You can do this in any order that makes sense to you, just be sure that all record formats with input fields (SFLCTL, ABOVE and BELOW) are processed by the program.
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 C WRITEABOVE C WRITEBELOW C EXFMTSFLCTL C READ ABOVE 90 C READ BELOW 91 ... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6
Create Your Own Edit Codes
One of the better features of OS/400 is the ability to create your own edit codes. Three that I use are one for a modified "Y" edit code, a social security edit code and a telephone number edit code. The edit code commands are Create Edit Description (CRTEDTD), Delete Edit Description (DLTEDTD), and Display Edit Description (DSPEDTD). There is no command to change an edit code description.
To create an edit code of your own, you must first delete one of the edit codes numbered 5-9 that IBM provides as samples (e.g., DLTEDTD 5). Then you can replace that code with one of your own. I will show you the three that I use, but I will use a lowercase "x" for the edit code. You can choose your own. The character "b" in the following commands represents a blank.
CRTEDTD EDTD(x) INTMASK('b0/bb/bb') + DECPNT(*NONE) ZEROBAL(*NO) + TEXT('Similar to Y, but no print if date = 000000') CRTEDTD EDTD(x) INTMASK('bbb-bb-bbbb') DECPNT(*NONE) + FILLCHAR(0) ZEROBAL(*YES) + TEXT('Social Security Edit Code') CRTEDTD EDTD(x) INTMASK('bbb)&bbb-bbbb') DECPNT(*NONE) + ZEROBAL(*NO) LFTCNS('(') TEXT('Telephone edit code')
Sending Messages from an RPG Program
Sending messages (even those that require a response) to a message queue from a RPG/400 program is simple with the Display (DSPLY) operation. Information messages, field values and reply messages can be sent to the message queue with this operation.
By using the DSPLY operation, one simple statement can notify the user through their message queue that a job has been aborted.
Here is the basic format of the statement (the compile-time array ARY is used to accomplish easy message text set-up):
...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 C ARY,1 DSPLYUSERMQ **
Extract Job Aborted
Factor 1 can contain a field name, a literal, a named constant, a table name or an array element whose value is to be displayed. Factor 1 can also contain *M, followed by a message identifier that identifies the message to be retrieved from the message file QUSERMSG. To use a different message file, use the Override Message File (OVRMSGF) command.
Factor 2 contains the name of the message queue to which the message will be sent. If the job is batch and factor 2 is not specified, QSYSOPR becomes the default queue. If the job is interactive and factor 2 is not specified, *EXT becomes the default queue.
The result field contains the field that is to accept the response (shown in a later example).
This statement will display user message USR0001 from message file QUSERMSG:
... 1 ...+... 2 ...+... 3 ...+ C *MUSR0001 DSPLY
The most powerful way in which to use the operation is to display a message and request a response. For example, you may want to give the operator the option to continue a batch job. The following statement will send the question, "Do you want to continue? (Y/N)" to the QSYSOPR message queue, and the program will wait until a response is given.
...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+ E MSG 1 1 80 C MSG,1 DSPLY RESP 1 **
Do you want to continue? (Y N)
There are some restrictions. You can DSPLY to any message queue except a program message queue. The maximum length of information that can be displayed is 52.
Controlling Spool File Names
Suppose you want to write an RPG program which will create print files with varying spool file names. Normally, the spool file name which is created is the same as the file name in the RPG program. You can give the spooled file any name you wish by specifying the spooled file name in the SPLFNAME parameter of the Override Print File (OVRPRTF) command. If you want to do it all in RPG, code UC in columns 71-72 of the F-spec for the printer file. Then run the OVRPRTF command using QCMDEXC before you open the printer file manually in the RPG program.
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 . FQPRINT O F 132 OF PRINTER UC I 'OVRPRTF QPRINT - C CMD I 'SPLFNAME(INVOICE)' C CALL 'QCMDEXC' C PARM CMD COMAND 50 C PARM 32 LENGTH 155 C OPEN QPRINT ... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 .
Calling with Less Parameters
RPG programs do not bomb when parameters defined in their *ENTRY parameter list are not passed-only when the unpassed parameters are referenced. This doesn't mean you have to always pass every parameter. There is a way to determine how many parameters have been passed. Set up a Program Status Data Structure (PSDS) and define a field for the special keyword *PARMS. You can code the PSDS as follows:
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 IPSDS SDS I *PARMS PARMS
Access the PARMS field to determine the number of parameters passed. Now your program can avoid referencing unpassed parameters.
Take Care of Decimal Data Errors
If you are plagued with decimal data errors (especially if you have migrated from a S/36) and you need a quick fix, try out this technique. You can compile your program with the compiler option, Ignore Decimal Data Error (IGNDECERR), set to *YES. Not only will it ignore the error, but if you are updating files it will initialize the numeric fields to 0. If you do have a file with decimal data errors that you would like to clean up quickly, a program to do this would be:
... 1 ...+... 2 ...+... 3 ...+... 4 ...+ FFILENAMEUP E DISK C UPDATRCDFMT Make sure to compile with IGNDECERR(*YES).
Default RPG Header Specifications
For RPG/400 programs, do not include an H-spec in the source member when you need special entries like an alternate collating sequence. Instead, in library QRPG, create a character data area, DFTHSPEC which is 80 characters long containing your installation's standard values for the currency symbol, date format and edit, and decimal notation. The Control specification (H in column 6) is not required. This allows global changes without changing any source members.
The DFTHSPEC data area can be overridden by including an H-spec in the source. It will also be overridden if a data area, RPGHSPEC, is found anywhere in the library list. This allows you to manipulate the library list to select an H- spec.
For example, you can activate the DEBUG option automatically for testing as follows. Leave column 15 blank in DFTHSPEC or production library versions of RPGHSPEC, but place a 1 in column 15 in your test library versions of RPGHSPEC. When you are through testing the program in the test library, simply recompile the program to the production library.
Commands in Compile-time Arrays
When calling QCMDEXC in an RPG program to execute a command, you might sometimes find it useful to define that command as a compile-time entry. An easy way to get the syntax of the command correct the first time in SEU is to use F13 to change the session defaults. Move down to the source type line, change it to CLP and press Enter. Type the CL command and press F4 to prompt. When done, be sure to use F13 again to change the source type back to RPG.
Multiple-occurrence External Data Structures
How many times have you had a program that had already retrieved a record and temporarily needed to get another record from the same file without overlaying the original field values?
The most widely used method of performing this task is to define identical fields and move the data back and forth. This technique performs the task intended, but it requires the fields to be hard-coded in the program. This effectively circumvents the database concept of dynamic fields and field definitions.
A multiple-occurrence external data structure will perform the same task without requiring the fields or their definitions to be coded into the program. Additionally, this technique only requires a few lines of code, regardless of the number of fields in the file.
In the following example, I define a data structure with two occurrences, referencing the file name for the subfield definitions. I place an OCUR statement to access the first occurence immediately before the initial read. I then place another OCUR statement to access the second occurrence immediately before the "temporary" read. Now the field values are safely stored away in the first occurrence of the data structure. After I am through processing the "temporary" record, and I am ready to restore the field values, I simply perform an OCUR operation to the first occurrence and the fields return to the values that they had before the "temporary" read was performed.
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 FCUSTMASTIF E K DISK ICUSTDS E DSCUSTMAST 2 C 1 OCUR CUSTDS C KLIST CHAINCUSTMAST 99 C 2 OCUR CUSTDS C KLIST CHAINCUSTMAST 99 ... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6
Setting Subfile Indicators
If you try to display a subfile with no active subfile records, an exception error occurs. A common approach to eliminate this problem is to condition the Subfile Display (SFLDSP) keyword with an indicator and set it on or off based on whether or not any records have been written to the subfile. There is an easy way to accomplish this in RPG. Simply move the resulting indicator from the first READ operation of the file you are processing to load the subfile, to the indicator which conditions the SFLDSP keyword. For example, the following line of code is from the subfile control record of a display file. Notice that indicator 26 is used to condition the SFLDSP keyword:
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 . A N26 SFLDSP
In the following excerpt from an RPG program, indicator 99 is used as the end- of-file indicator on the first READ statement. After the read statement, indicator 99 is moved to indicator 26 which conditions the SFLDSP keyword. The net result is that if end-of-file is detected on the first read, then the subfile will not be displayed.
... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 * Position file as requested C SCRFLD SETLLDBFILE C SCRFLD READEDBFILE 99 * Set SFLDSP indicator C MOVE *IN99 *IN26 C *IN99 DOWEQ*OFF * * Load subfile fields... * C WRITESFLREC C SCRFLD READEDBFILE 99 C END C EXFMTSFLCTL ... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6
LATEST COMMENTS
MC Press Online