Midrange News for the IBM i Community


Posted by: Tim Brinker
Variable Expression
has no ratings.
Published: 10 May 2012
Revised: 23 Jan 2013 - 3715 days ago
Last viewed on: 25 Mar 2023 (5183 views) 

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.

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

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: DaleB
Premium member *
Reading, PA
Comment on: Variable Expression
Posted: 10 years 10 months 16 days 21 hours 7 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.

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Variable Expression
Posted: 10 years 10 months 16 days 21 hours 7 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.

Posted by: tbrinkereaglewindowcom
Premium member *
Comment on: Variable Expression
Posted: 10 years 10 months 16 days 20 hours 23 minutes ago

Thanks for the quick response.

 

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

Posted by: TFisher
Premium member *
Comment on: Variable Expression
Posted: 10 years 10 months 16 days 13 hours 29 minutes ago

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

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Variable Expression
Posted: 10 years 10 months 16 days 9 hours 17 minutes ago

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