## Solve Arithmetic Expression using PARSE in REXX

IBM's Command List programming language & Restructured Extended Executor

### Solve Arithmetic Expression using PARSE in REXX

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
ravikommuri

Posts: 3
Joined: Mon Feb 04, 2013 11:55 pm
Has thanked: 0 time
Been thanked: 0 time

### Re: Solve Arithmetic Expression using PARSE in REXX

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.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator

Posts: 2515
Joined: Sun Jul 04, 2010 12:13 am
Location: Down on the pig farm
Has thanked: 4 times
Been thanked: 86 times

### Re: Solve Arithmetic Expression using PARSE in REXX

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator

Posts: 2501
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 119 times

### Re: Solve Arithmetic Expression using PARSE in REXX

Hi NicC,

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 )

Thank you
ravikommuri

Posts: 3
Joined: Mon Feb 04, 2013 11:55 pm
Has thanked: 0 time
Been thanked: 0 time

### Re: Solve Arithmetic Expression using PARSE in REXX

no need to strip A
" and ' are the string delimiters and they are not included in the string DATA
why not try it Yourself

http://sourceforge.net/projects/oorexx/

it will let You exercise REXX without a <MF connection>
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator

Posts: 2501
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 119 times

### Re: Solve Arithmetic Expression using PARSE in REXX

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
ravikommuri

Posts: 3
Joined: Mon Feb 04, 2013 11:55 pm
Has thanked: 0 time
Been thanked: 0 time