Solve Arithmetic Expression using PARSE in REXX



IBM's Command List programming language & Restructured Extended Executor

Solve Arithmetic Expression using PARSE in REXX

Postby ravikommuri » Tue Feb 05, 2013 12:07 am

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

 

Re: Solve Arithmetic Expression using PARSE in REXX

Postby NicC » Tue Feb 05, 2013 1:12 am

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: 2690
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisys (almost)
Has thanked: 4 times
Been thanked: 105 times

Re: Solve Arithmetic Expression using PARSE in REXX

Postby enrico-sorichetti » Tue Feb 05, 2013 1:31 am

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: 2643
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: Solve Arithmetic Expression using PARSE in REXX

Postby ravikommuri » Tue Feb 05, 2013 12:55 pm

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
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

Postby enrico-sorichetti » Tue Feb 05, 2013 1:06 pm

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>
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: 2643
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: Solve Arithmetic Expression using PARSE in REXX

Postby ravikommuri » Tue Feb 05, 2013 3:01 pm

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


Return to CLIST & REXX

 


  • Related topics
    Replies
    Views
    Last post