how to have multiple replace statements when using String manipulation

Hi there,
I am trying to replace the multiple strings in Column1 with different names.

Column1
a
b
c
d

My expression in String manipulation node is:
replace($Column1$, “a”, “aa”);
replace($Column1$, “b”, “bb”)

I am getting this error message:
Invalid settings:
org.knime.ext.sun.nodes.script.compile.CompilationFailedException:Unable to compile expression
Unreachable coe.

Hi @ivy_wang , welcome to the KNIME community.

The string manipulation node cannot accept multiple statements in the sense that they execute one after the other but it is possible to combine some statements together by “nesting” them,

e.g.

replace(
   replace($Column1$, "b", "bb"),
$Column1$, "a", "aa")

However this would have the effect of performing multiple replacements on strings like this:
image

I don’t know if that is what you want, but if it is then that’s great! :wink:

An alternative can be achieved using this syntax:

string(
$Column1$.equals("a")
?"aa"
:$Column1$.equals("b")
?"bb"
:$Column1$
)

which reads as "if Column1 =“a” then return “aa”, otherwise if Column1 = “b”, return “bb” otherwise return the value unchanged.

(and you could also place commands such as replace() instead of literals such as “aa” or “bb” )

The result would be:
image

1 Like

Hi,

thank you so much, I think the second approach solved my issue.

Does knime support any formula like if else? Like the ‘key’ node in Alteryx. Thanks!

Hi @ivy_wang , if you are looking to write more scripted formulas, the Column Expressions and Variable Expressions nodes are available within the “Knime Expressions” extension, and use a form of javascript. These are (kind of) similar to the Alteryx “formula” tool:

For simple replacements such as the example you gave, the Rule Engine would be well suited.
This would have been written as

$Column1$ = "a" => "aa"
$Column1$ = "b" => "bb"
TRUE => $Column1$

The rule engine’s syntax looks a little odd but each line gives an expression (a rule) followed by => followed by the resultant output if the rule equates to TRUE. It simply actions the first rule in the list that equates to TRUE for each row in the column being processed.

The downside of Rule Engine is that it cannot perform any calculations or string processing (such as concatenation, substrings etc). It simply handles column values and literals. But it is useful for straightforward replacements such as the above.

The core Java Snippets and Java Edit Variables nodes also allow for the writing of expressions, if you really want to dig into a little programming :wink: especially if you know a some Java :slight_smile: but these take a bit more getting used to.

(and there are the Python nodes too, if you know python. Unlike the other nodes mentioned here which process one row at a time, progressing down the table, the Python nodes give you access to the entire table as a dataframe so if you know python they can be pretty powerful and achieve things that the other nodes cannot easily do)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.