Page 1 of 1

Evaluate Arithmatic Expression coming in input string

Posted: Mon Feb 11, 2019 1:23 am
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 ?

Posted: Tue Feb 12, 2019 1:17 am
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

Posted: Tue Feb 12, 2019 2:45 am
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.

Posted: Tue Feb 12, 2019 8:49 am
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.

Posted: Tue Feb 12, 2019 5:52 pm
by chulett
Sweet.

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

Posted: Wed Feb 13, 2019 12:16 am
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..

Posted: Wed Feb 13, 2019 12:24 am
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?

Posted: Wed Feb 13, 2019 1:31 am
by qt_ky
That I don't know. Sounds like a custom routine may be a better fit.

Posted: Wed Feb 13, 2019 1:43 am
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

Posted: Wed Feb 13, 2019 4:38 am
by UCDI
are they all of the same format, like A operator B operator C?

Posted: Wed Feb 13, 2019 10:48 pm
by goutam
No..it varies..Somewhere we have one operator and somewhere it's more than one..max no of operator is 5.

Posted: Thu Feb 14, 2019 4:13 pm
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.

Posted: Fri Feb 15, 2019 2:52 am
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.

Posted: Thu Feb 21, 2019 3:18 am
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.