You are currently on IBM Systems Media’s archival website. Click here to view our new website.

IBM i > DEVELOPER > RPG

Even More Flexible Calculations

In the September EXTRA e-Newsletter, Jon Paris and Susan Gantner shared some solutions to “unsolvable” RPG problems with a technique for flexible calculations. Now they delve deeper with a more complex example, making a sequence of calculations not only possible but simple to create and use.


As promised in our previous article, “How Flexible are Your Calculations?,” it’s time to take our RPG problem solving to the next level. Performing a single calculation is useful, but it’s even better to be able to perform a sequence of calculations. And as anyone who’s ever done any programming is aware, any sequence of calculations is likely to require other operations, such as copying a value or making comparisons to determine the result. So before we discuss the adaptations in methodology, let’s take a few minutes to study the display screen used to test our modified approach (see Figure 1).

The screen is divided into two areas. At the top are the names of the available fields and their values. Below that, you can see the input area for the operations. Although this input area may look like an obvious candidate for a subfile, we decide instead to use an array, simply because in a “real” application of this methodology, the calculation data would probably be loaded from a database, not keyed in as it is in this test harness.

How It Works

Enter the calculation details (up to six operations in the demo program), press enter and voila! The calculations are performed and the resulting changes in the field contents displayed. Then enter your next calculation sequence, and continue ad nauseam until you get bored and press F3 to exit the program.

Having looked briefly at the screen layout, let’s see how it’s mapped in the program. We use an externally described data structure (DS) taking advantage of V5R2’s capability to request just the input fields. This is the default behavior (what you get when no qualifier is specified and what you always got prior to V5R2). We mention it because we’ve also used the same approach to define the output fields (by using the *Output qualifier), and that wasn’t possible in earlier releases.

The main point of interest in the DS definition is the group field calculations, which enables us to process all of the calculation details as an array. If this technique isn’t familiar to you, you can find out about it by reading our article “D-spec Discoveries.”

d mainInp      E DS          Extname(VarFuncTst: *Input)

d calculations               Overlay(mainInp) Dim(6)
d res                    10a Overlay(calculations)
d op                      2a Overlay(calculations: *Next)
d f1                     10a Overlay(calculations: *Next)
d f2                     10a Overlay(calculations: *Next)
d f3                     10a Overlay(calculations: *Next)
d f4                     10a Overlay(calculations: *Next)

 

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.



Advertisement

Advertisement

2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters