Double iteration to replace text

Hi all,

Wondering if somebody can give me a clue on how to implement this,
I have two tables:
Table A: contains a single column as string
Table B: contains two columns (string both), “original” column has a value to look for and “replace” column has the correspondent value to replace.

The intention is to iterate over Table A and iterate also on Table B, if any of the strings in Table B “original” text is found in the row of Table A, then the text will be replaced by Table B “replace”.

Consider that in Table A, the record can contain more than one text that needs to be replaced, so for each row in Table A, the process needs to scan all Table B and replace the text if any is found.

Pseudo example:
Table A
This is the first row
I am also a text here
This row is also a text

Table B:
Original Replace
row ROW
text TEXT

Outcome should be:
Table A
This is the firs ROW
I am also a TEXT here
This ROW is also a TEXT

Tried to implement with Recursive Nodes and nesting but couldn’t make it

Appreciate any help!

Hi @eamador

Welcome to the KNIME forum!

This issue has been raised quite a few times before. Below is an example of a common Recursive Loop based approach:

Within the loop, the values of each row in Table B are passed along and used as variable in the String Manipulation node whereby the text replacement is arranged. At the end, only the last iteration is collected after all rows of Table B have been processed.

See WF:
Double iteration to replace text.knwf (36.1 KB)

Hope this helps!

1 Like

Thanks ArjenEX! this is exactly what I was looking for!

1 Like

Hi @eamador @ArjenEX , it must be quite slow to go through each lookup one by one through that loop, and it will grow even slower as the number of lookups increases.

@eamador any reason why you want to do this using loops?

Here’s a more efficient way to do this kind of replace using the dictionary approach:

Table A (same as yours):

Table B (same as yours, with an extra item):


Here’s the workflow: Replace words using lookup table.knwf (11.9 KB)

1 Like

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