Variable Expression
Published by: **Tim Brinker** on 10 May 2012 view comments(5)

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

(Sign in to Post a Comment)

Comment on: Variable Expression

Posted: 6 years 6 months 10 days 1 hours 48 minutes ago

Posted: 6 years 6 months 10 days 1 hours 48 minutes ago

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.

Comment on: Variable Expression

Posted: 6 years 6 months 10 days 1 hours 47 minutes ago

Posted: 6 years 6 months 10 days 1 hours 47 minutes ago

Yes and no.

You'd need to use embedded SQL and use (probably) the SET followed by execute immediate... have to think about that.

Comment on: Variable Expression

Posted: 6 years 6 months 10 days 1 hours 3 minutes ago

Posted: 6 years 6 months 10 days 1 hours 3 minutes ago

Thanks for the quick response.

I believe SQL will work for what I am trying to do.

Comment on: Variable Expression

Posted: 6 years 6 months 9 days 18 hours 10 minutes ago

Posted: 6 years 6 months 9 days 18 hours 10 minutes ago

Isn't there a C function that will allow this?

Comment on: Variable Expression

Posted: 6 years 6 months 9 days 13 hours 58 minutes ago

Posted: 6 years 6 months 9 days 13 hours 58 minutes ago

No. There is however a Java!--script-- instruction that will do this.