26
Fri, Apr
1 New Articles

HOT TIPS: RPG/400 (12 Tips)

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

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 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • 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.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • 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

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • 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: