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

IBM i > DEVELOPER > RPG

How Flexible are Your Calculations?

Industry experts and IBM Systems Magazine, i5 Business Edition technical editors, Jon Paris and Susan Gantner share some “out-of-the-box” ideas for solving “unsolvable” problems in RPG.


 

In this EXTRA article, we want to introduce some RPG programming techniques and hopefully make you think about ways to combine them to solve “unsolvable” problems. One such case is how to provide user-controlled, spreadsheet-style calculations within an RPG program.

We’ve addressed this problem several times in the past, but on this occasion the trigger was a request during a customer training class for a simpler approach to pricing calculations. The customer’s existing solution was inflexible and required significant effort to introduce new pricing schemes. The end users (bless their little cotton socks) simply wanted to enter the names of the variables and the required calculation steps entered into a database. The program would then retrieve these details, apply them to a set of data and report the results. The calculation steps were very similar to how we used to perform math in RPG/400 (e.g., add two variables, store the result in a temporary field, then multiply the temporary by a third field and so on). Had it been an option to write the new program in PHP, for example, this requirement wouldn’t have presented much of a problem – such tasks are simpler to handle in an interpreted language. But, naturally, the required solution needed to be integrated into an RPG application!

While a comprehensive solution to problems like this isn’t really practical, RPG provides the basic tools to handle the requirement providing the problem space is constrained. We'll discuss what we mean by that later. The problem has two basic elements. First, we need to be able to select the actual calculation performed (add, subtract, etc.) based on the database entry. Second, we need to perform the selected calculation on fields whose names were also supplied in the database entry.

Let's look at the calculation aspect first. Suppose you wanted to accommodate the basic add, subtract, divide and multiply operations. You might be tempted to do something like this:

If operation = add;
    result = var1 + var2;
 ElseIf operation = subtract;
    result = var1 - var2;
 ElseIf ...

 

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