Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

replacing string

hi guys

i have a few formulas one of them  look like - (sum(x)+avg(z))/sum(y)

I need to change the formula to a set analysis something like -

sum({<date={"<$(Max2)>$( Max1)"}>}+ avg({<date={"<$(Max2)>$( Max1)"}>}/

sum({<date={"<$(Max2)>$( Max1)"}>}

I managed to do it with the "replace()" func  -

replace(String,'sum(','sum({<date={"<$(Max2)>$(Max1)"}>}')

But I need some kind of “replaceif” condition that will check if the string is  ‘sum(‘ or ‘avg(’

if((string='sum(', 

     replace(string,'sum(','sum({<date={"<$(Max2)>$(Max1)"}>}'),
if((string)='Avg(',
          
replace(string,'Avg(','Avg({<date={"<$(Max2)>$(Max1)"}>}'),

any ideas?

THX

J

1 Solution

Accepted Solutions
Jason_Michaelides
Luminary Alumni
Luminary Alumni

Use MapSubstring:

Map_Expressions:

MAPPING LOAD * INLINE [

In,Out

sum(,sum({<date={"<$(Max2)>$(Max1)"}>}

avg(,avg({<date={"<$(Max2)>$(Max1)"}>}

max(,max({<date={"<$(Max2)>$(Max1)"}>}

];

Then use

MapSubstring('Map_Expressions',String)     AS     Expression

Hope this helps,

Jason

View solution in original post

11 Replies
Jason_Michaelides
Luminary Alumni
Luminary Alumni

I assume this is in the script?  If so you can use MapSubString.

Hope this helps,

Jason

Anonymous
Not applicable
Author

nope this is in the Gui

tresesco
MVP
MVP

Isn't your second expression(with proper braces) working?

if((string='sum(',

     replace(string,'sum(','sum({<date={"<$(Max2)>$(Max1)"}>}'),
if((string='Avg(',   // Edit: brace removed
          
replace(string,'Avg(','Avg({<date={"<$(Max2)>$(Max1)"}>}')

    )

)

Anonymous
Not applicable
Author

no cause i don't know in advance the index for the 'sum(' or 'avg('

plus my expression will only work on the sum( it wont work on both sum( + avg(

tresesco
MVP
MVP

Can you please explain your exact requirement with a sample?

Anonymous
Not applicable
Author

I have a string field named formula,  this field has all kinds of formulas  for example :


(sum(x)+avg(z))/sum(y)

Or

sum(y)


In order to make a certain calculation I need to use set analysis so I used the ‘replace()’ string function for example

replace(String,'sum(','sum({<date={"<$(Max2)>$(Max1)"}>}')

My problem is I don’t know in advance how the formula will look. it could have a sum function or avg  function or max function  or both…

I need to check the string  and replace each string that looks like’ sum(‘ or’ avg(‘ or ‘max(

To :

'sum({<date={"<$(Max2)>$(Max1)"}>}’ or ‘avg({<date={"<$(Max2)>$(Max1)"}>}’ or

max({<date={"<$(Max2)>$(Max1)"}>}

tresesco
MVP
MVP

May be using REPLACE recursively, like:

replace(

     replace(

               replace(String,'avg(','avg({<date={"<$(Max2)>$(Max1)"}>}')),

                'sum(','sum({<date={"<$(Max2)>$(Max1)"}>}')),

                'max(','max({<date={"<$(Max2)>$(Max1)"}>}'))


       

        


Jason_Michaelides
Luminary Alumni
Luminary Alumni

Use MapSubstring:

Map_Expressions:

MAPPING LOAD * INLINE [

In,Out

sum(,sum({<date={"<$(Max2)>$(Max1)"}>}

avg(,avg({<date={"<$(Max2)>$(Max1)"}>}

max(,max({<date={"<$(Max2)>$(Max1)"}>}

];

Then use

MapSubstring('Map_Expressions',String)     AS     Expression

Hope this helps,

Jason

Anonymous
Not applicable
Author

could you give an example i'm not familiar with this function

thanks alot

J