create computations on the fly

Application development
Programming Languages
Retail/point-of-sale applications
Web development
We are primarily a COBOL shop. We have some COBOL ILE and some RPG. I have a need to replicate a function on the I-series that is currently done in Excel. This function allows them to create formulas on the fly to arrive at pricing information. I can work through issues with interactive maintenance of the formulas. Has anyone ever built COMPUTE or arithmetic statements on the fly inside of a program? Is so can you provide some insight on how you implmented? Appreciate any information. Thanks!

Answer Wiki

Thanks. We'll let you know when a new response is added.

Cobol is compiled, so you’re not going to get much on the fly. One way to do this is to parse through your input and execute it directly. That works every time, but is time-consuming to write. A second way is a have a collection of stored methods, with names or numbers, and execute the one specified. The down-side here is that you will be adding to the program from time to time, and ad hoc queries won’t be handled.

Sheldon Linker (
Linker Systems, Inc. (
800-315-1174 (+1-949-552-1904)


Formulas and variable values can be handled dynamically by REXX.

You might create a basic REXX procedure that simply retrieves strings from the REXX external queue and processes the strings with the INTERPRET statement. The procedure can be invoked with the STRREXPRC CL command or with the QREXX API. Load a series of strings onto the queue with the QREXQ API first in the form “X = 2” and “Y = 3” to set variables. Then queue a string like “Z = X + Y”.

REXX will interpret those and execute the “formula”.

Finally, queue a string like “QUEUE Z” to have REXX place the result back on the REXX external queue. Use QREXQ to retrieve the result. (Or access the REXX variable pool with QREXVAR if you’re adventurous.)

Since REXX can dynamically generate its own source statements and execute them, it’s pretty flexible. All the REXX you really need is the initial REXX procedure to accept strings and execute them in a general loop. Pass some final special string like “ExitRexx” to know when to leave the loop and return from REXX.


Discuss This Question: 6  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • BeerMaker
    yes, we have done something similar that may be helpful. Its basically a compiler and scripting language that creates machine code you can call at runtime. The users can generate script modules the would be compiled and linked in to other programs. Since it generates machine code its extremely fast for high volume transactions. If you'd like to talk about licensing this technlogy or getting some assistance send me a private message.
    0 pointsBadges:
  • Walter47
    I'm sure you are using CL to execute this application. I've done similar by passing parameters to the executing program.
    120 pointsBadges:
  • Bigaub
    Use dynamic SQL. This allows you to do calculations on the run.
    0 pointsBadges:
  • Whanzens
    I once (in 1990) wrote a program to calculate the value of an arithmatic statement. This program is written in RPG III and has 3 parameters, the statement (255A), the result (30,9) and an error code (2A). The statement can consist of the following signs, +, -, /, *, ^, (, ), SQR() for square root with numbers everywhere in it. If you're interested, send me a private message.
    0 pointsBadges:
  • BeerMaker
    our solution uses a proprietary scripting language and compiler to generate machine code. Assembly modules dynamically load these functions from the Loadlib based on VSAM table entries then the calculations are loaded and executed. The script language provids access to fields in our system and results are passed back in return values to be processed by cobol programs in a batch process. This is part of a proprietary system that has been running nightly at several customers for about 8+ years. If you can use Dynamic SQL or a stored proc it would be a good option. We were limited to mainframe and VSAM, but needed a scripting/parsing solution that changes based on customer input. If you have a front end that is a Windows EXE there are add-ons that can use VB and you can run VB on the fly in your front end.
    0 pointsBadges:
  • Kccrosser
    One note on security when using Dynamic SQL to execute instructions - you should provide means to ensure that the submitted string doesn not contain any malicious SQL code. Any time you create a pass-through function to Dynamic SQL, you potentially allow your system to be compromised. In this case, if the input text was only expected to be an arithmetic expression, you could simply parse for the existence of any alphabetic characters. When the input can contain alpha characters, the problem is a bit more difficult, but you should never allow raw input to be sent to Dynamic SQL to be executed. At the very least, parse for the existence of a semi-colon statement delimiter.
    3,830 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: