Evaluate Arithmatic Expression coming in input string

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
goutam
Premium Member
Premium Member
Posts: 109
Joined: Thu Jul 26, 2007 6:53 am

Evaluate Arithmatic Expression coming in input string

Post by goutam »

HI All,

I have an input column which holds arithmetic expressions e.g. [(20*5)+9], [(30/5)+4]. I need to evaluate them and send the final value to target system. Any way to achieve this ?
Goutam Sahoo
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

Seems like an unusual requirement...

The first thing that comes to mind is to take the expression and put in into a UNIX Korn shell for evaluation inside of the $(( )) syntax:

Code: Select all

$ echo $((4+5))
9

$ echo $(((20*5)+9))
109

$ echo $(((30/5)+4))
10

$ echo $((7 / 0))
ksh: 7 / 0: 0403-056 Cannot divide by zero.

$ echo $((4 m 3))
ksh: 4 m 3: 0403-057 Syntax error
Choose a job you love, and you will never have to work a day in your life. - Confucius
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Or perhaps "bc" at the command line? Or something using C++?

https://stackoverflow.com/questions/342 ... tring-form

In any case... not fun. Leverage the work of others.
-craig

"You can never have too many knives" -- Logan Nine Fingers
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

I ran a quick test file with expressions through an External Filter stage with 'bc' as the filter command and got correctly evaluated outputs.
Choose a job you love, and you will never have to work a day in your life. - Confucius
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Sweet.

Question for Goutam, what kind of processing volume are we talking about here?
-craig

"You can never have too many knives" -- Logan Nine Fingers
goutam
Premium Member
Premium Member
Posts: 109
Joined: Thu Jul 26, 2007 6:53 am

Post by goutam »

HI Chulett, Thanks for your response. Actaully we are getting 30k records on a daily basis and there are 20 fields like this which we need to evaluate..
Goutam Sahoo
goutam
Premium Member
Premium Member
Posts: 109
Joined: Thu Jul 26, 2007 6:53 am

Post by goutam »

Thanks a lot, qt_ky. If i need to evaluate multiple columns having arithmetic expressions then do i need to use that many no of external filter stages?
Goutam Sahoo
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

That I don't know. Sounds like a custom routine may be a better fit.
Choose a job you love, and you will never have to work a day in your life. - Confucius
JRodriguez
Premium Member
Premium Member
Posts: 425
Joined: Sat Nov 19, 2005 9:26 am
Location: New York City
Contact:

Post by JRodriguez »

Play around to force the evaluation of the string input as arithmetic by using the AsInteger, AsDouble, AsFloat functions. Just pass the input column to one of these and that might do the trick... You might need to enforce the result by multiplying by a constant factor like below....just play around


AsFloat(input arithmetic) * 1


I wonder if in your site the use of Server jobs is allowed? In the old server job any string arithmetic expression will be evaluated as arithmetic plain and simple. You would only need the input column as a derivation to the target column.

Let us know how it goes
Julio Rodriguez
ETL Developer by choice

"Sure we have lots of reasons for being rude - But no excuses
UCDI
Premium Member
Premium Member
Posts: 383
Joined: Mon Mar 21, 2016 2:00 pm

Post by UCDI »

are they all of the same format, like A operator B operator C?
goutam
Premium Member
Premium Member
Posts: 109
Joined: Thu Jul 26, 2007 6:53 am

Post by goutam »

No..it varies..Somewhere we have one operator and somewhere it's more than one..max no of operator is 5.
Goutam Sahoo
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Somewhat unrelated. Recently I was installing 11.7 IIS with Enterprise Search, and the delivered Linux server did not have bc installed. That actually caused the installation to fail. Checking for it has since been updated into IBM's installation documentation.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Well, haven't put much thought into this to be honest but you may want to take a shot at seeing how shelling out to use "bc" on each row will work for you. 30K isn't really that large of a volume but the context switching or whatever overhead is added by leveraging something on the command line may or may not be acceptable to your load timings. But seems to me should be the easiest to test. If the overhead is too much then look into something more tightly coupled into the parallel framework, something custom in C++ for example.

What IS your source, by the way? You've not said. Flat file? If so then maybe you could work out a "filter" that would do that for you.
-craig

"You can never have too many knives" -- Logan Nine Fingers
goutam
Premium Member
Premium Member
Posts: 109
Joined: Thu Jul 26, 2007 6:53 am

Post by goutam »

HI Rodriguez,

I tried with functions like AsInteger(), AsFloat() and AsDouble(). Nothing is working as they expect numeric string But we have arithmetic operators in the input string given to above functions.
Goutam Sahoo
Post Reply