Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
Is it possible to have a variable expression in RPG?
For example I can do the following:
Result = %Dec(5+2*2: 10: 3);
I want to do the following:
Equation = '5+2*2';
Result = %Dec(%Trim(Equation): 10: 3);
This fails with the following error "A character representation of a numeric value is in error (C G D F)." The string is being converted to a numeric value instead of being solved.
Any ideas on how to accomplish this?
Thanks,
Tim
So the %DEC is secondary. What you really want is a subprocedure that evaluates a string containing an infix expression, and returns a numeric result:
Equation = '5+2*2';
Result = %DEC(evalExpression(Equation): 10: 3);
Anyone have an example of this? There's probably a way to do it with embedded SQL, along the lines of SELECT 5+2*2 FROM SYSIBM.SYSDUMMY, but you'd have to do it dynamically, since you don't know the string ahead of time.
Yes and no.
You'd need to use embedded SQL and use (probably) the SET followed by execute immediate... have to think about that.
Thanks for the quick response.
I believe SQL will work for what I am trying to do.
Isn't there a C function that will allow this?
No. There is however a Java!--script-- instruction that will do this.