Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compute Arithmetic Expression Syntax Question 1

Status
Not open for further replies.

rasETL

IS-IT--Management
May 28, 2003
213
US
Can a COMPUTE Statement include relational operators that are stored in working-storage fields, or do they have to be literals in the statement.
For example, can the field WS-REL-OPER1 contain the operator "+", and the statement read...
COMPUTE RESULT = FLDA WS-REL-OPER1 FLDB
I am looking for a way to allow a calculation to be performed using data stored in a file, so the calculation field values and operators depend on data read from records. I haven't run this though a compiler yet.
 
Hi,

It is possible to write an interpreter for that. I made one once. I think even that there is an example-calculator from Micro Focus with one of the old compilers of them.

Maybe you can let SQL do the interpreting.

Regards,

Crox
 
If the operators are restricted to "+" and "-", you can do it this way:

77 WS-REL-OPER1 Pic S9.

IF operator should be "+"
MOVE +1 TO WS-REL-OPER1
ELSE
MOVE -1 TO WS-REL-OPER1
END-IF

. . .

COMPUTE RESULT = FLDA + WS-REL-OPER1 * FLDB

 
Thanks for the idea webrabbit, but unfortunately the operator can be any one of: + - * /
I was hoping to set up a "generic" compute which would be data-driven based on the information read from a record.
Alas, I will have to check for the value of the operator in the record and code 4 different COMPUTE Statements.
 
How's about this....
Enjoy Bob

EVALUATE ws-rel-oper1
when "+" compute result = afld + bfld
when "-" compute result = afld - bfld
when "*" compute result = afld * bfld
when "/" compute result = afld / bfld
END-EVALUATE.
 
If you can have any number of operators and operands, read up on Reverse Polish Notation. It is based on an algorithm developed by a Polish mathematition which allows any computation to be specified without parentheses. "Reversed" because it is easier for computers to work the algorithm backwards.
 
If the expression is like:

a + b * c

or

a + b * (c + d * e)


you need a better algorithm. Looking at the '+' or '-' you need to know what the next operator is. If it is '+' or '-', you know you can calculate the result between. If the next operator is '*' or '/' you have to do the calculation of the first operator later.


 
Crox - you are correct, however my idea was to setup a generic Compute that would handle multiple mathematical equations as a series of single equation "steps" - thus the reason I wanted the relational operator to be a working-storage field. That way, the Compute could be X number of steps, defined by the User.
 
webrabbit - While RPN is great for the computer, it requires that Users become educated in the RPN way - something I do not think will fly in this situation, nor at the company where I am on assignment. Thanks for your help anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top