Page 1 of 1

Solve Arithmetic Expression using PARSE in REXX

PostPosted: Tue Feb 05, 2013 12:07 am
by ravikommuri
Hi,

I am very new to REXX and started learning.

Can anybody help me how can I solve an Arithemtic expression received as String in REXX

Input : expr = '2+3*4+8'
Output : 22 ( means 2+(3*4)+8 )
or it can be a calculator answer 28

Re: Solve Arithmetic Expression using PARSE in REXX

PostPosted: Tue Feb 05, 2013 1:12 am
by NicC
First - welcome to the forum
Second - it helps to read the forum rules first before posting (I have moved your post from some totally inappropriate place to the correct place)
Thirdly - eveything in Rexx is a string and the normal mathematical rules are observed. If you are simply wanting to remove the single quotes to make expr an unquoted string then try TRIM. Look it up in the manual - available through the manuals link or by Google.

Re: Solve Arithmetic Expression using PARSE in REXX

PostPosted: Tue Feb 05, 2013 1:31 am
by enrico-sorichetti
calculator arithmetic is NOT arithmetic,
it carries on computations strictly left to right without obeying the precedence rules

It would have cost You very little to test the same with a simple rexx script ..

/* rexx */
say 2+3*4+8
exit

Re: Solve Arithmetic Expression using PARSE in REXX

PostPosted: Tue Feb 05, 2013 12:55 pm
by ravikommuri
Hi NicC,

Thank you for your reply

Now I will read the forum guidelines

I tried to TRIM the string using STRIP function

But the string remains as string.

For example

/* REXX */
EX = '2+3*4+5'
A = STRIP(EX,,"'")
D = A
B = 2+3*4+5
SAY 'D = ' D
SAY 'B = ' B

Here Output is

D = 2+3*4+5
B = 19

My required output is

D = 19 ( like B )

Please help me

Thank you

Re: Solve Arithmetic Expression using PARSE in REXX

PostPosted: Tue Feb 05, 2013 1:06 pm
by enrico-sorichetti
no need to strip A
" and ' are the string delimiters and they are not included in the string DATA
why not try it Yourself

read the REXX manuals about the interpret instruction

I strongly suggest to download and install on Your PC open object rexx available here
http://sourceforge.net/projects/oorexx/

it will let You exercise REXX without a <MF connection>

Re: Solve Arithmetic Expression using PARSE in REXX

PostPosted: Tue Feb 05, 2013 3:01 pm
by ravikommuri
Hi enrico,

Thank you for your valuable suggestion.

INTERPRET instruction worked well in my case.

I got the required output

Code is like below.

/* REXX */
INPT = '2+3*4+5'
EXPR = '='INPT
INTERPRET RESULT EXPR
SAY RESULT

Thanks again