Loop using updated value from Math Formula

Hello friends,

I am using Math formula ($Value$-1) within a Loop. I need the Value to get updated on every loop.

Input:
image

Expected Output:
image

Here is what I am getting right now my workflow:

image

Current workflow:
KNIME_project3.knwf (11.2 KB)

Hi @N904808 , unfortunately the looping doesn’t work quite like that.

For ease of reference, here is a screenshot of your workflow
image

So what will happen here is that your Table Creator has a single cell in a column called Value, and it has a value of 5.
Your counting loop is set to process 4 times (iterations 0 to 3) and each time through the loop, it will act on the row that is passed in at the beginning :

image

So the Math formula node, which you have set to say $Value$ - 1
image
will receive the value 5 and subtract 1 from it on every iteration, and from that, generate the row containing 4. That is why you see a 4 on every row.

To achieve the output you require, you could code it with a variable, but whilst this will work in this case, I’m not sure that it is exactly what you are looking for as think what you are hoping for is something that will effectively work on whatever the previously created row contained rather than the value of a variable that is being maintained within the loop.

Here, a flow variable is given the value of the “Value” column, prior to the loop starting. The Math Formula (Variable) node subtracts 1 from the variable on each iteration:

image

and then the Math Formula node applies that value to the “Value” column for this iteration
image

For this to work, however, it is important that on the loop end node, the “Propagate modified loop variables” is ticked, otherwise the flow variable value will be reset to its initial starting point for each iteration and you will end up with 4 for every row.

image

You will then get the result that you were looking for
image

Using changing variable value in loop.knwf (17.0 KB)

Like I said though, whilst this provides you with the required result in this case, I suspect that you may be looking for a solution to what is really a slightly different use case, and you were using this as a simple example, and in that different use case this will not work, but I’ll leave it at that for now rather than trying to second-guess, as this may be all you require.

Edit: I suspect that you may be wanting something like the recursive loop which I have attached here:

Using changing value in loop - with recursive loop.knwf (25.8 KB)

The recursive loop is the loop construct in KNIME that allows each iteration to act on a newly updated version of the table from the previous iteration.

2 Likes

Beside @takbb great and detailed solution you might also want to try to subtract the current interation value in math formula using a generic loop with conditional end

br

2 Likes

@takbb The recursive loop is exactly what I needed. Thank you for helping with my basic KNIME questions and detailed explanations.

2 Likes

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