Concatenate strings to form given variables in java snippet

Hi all,

I’m trying to loop through variable names to substract corresponding columns from each other. Instead of adding a bunch of math formulas for each pair of columns i want to realize it with java snippet, because it seem more elegant and efficient. The table header looks like this:


(sorry i don’t know how the formatting works here to make a table)

Since the variable names are given by java snippet (c_A, out_A etc.) i thought i could easily loop through an array i created out of the first half of the header to achieve what i want to do. I’m thinking about a forEach loop iterating through said array which builds the variables names in each iteration. Something like:

var array= v_header;
forEach (i in array) {
let a = "c_" + i;
let b = "c_" + i + "1";
let c= "out_"+i;

(v_header is [“A”,“B”,“C”,…])

The loop itself might not work, because i’m still learning and might have to find another way, so ignore that aspect. But before i can try building a forEach loop i need to make java snippet understand that the given variables are build from strings. I tried to build the variable names without the loop and array (like var a = “c_” + “A”) which made me understand that the problem doesn’t lie in the loop but in the fact that the resulting variable isn’t congruent with the variables set by knime, but just another string.
I read about eval() which is not understood by java snippet (method is undefined) and shouldn’t be used anyway for some reason. But the alternatives (window or Function()) are also undefined or i don’t seem to grasp how to use them correctly.

I’m also not sure if all of this is a javascript related question or a knime related one, so i decided to post it here first instead of stackoverflow. Sorry if this is the wrong place.

Oh, and i’m using 4.2.5

Thanks in advance :slight_smile:

I just noticed i’m looking for JS solutions while the java snippet uses java :confused:

I’m trying to solve my problem with looping through a math formula now. Help would still be appreciated.

hi @gonzo0815 ,
I’ve tried to solve the problem. Here’s my workflow (maybe overcomplicated), please tell me if it suits your needs
KNIME_project7.knwf (29.5 KB)

1 Like

Thanks for trying to help me, but my ability to download extensions is dependend on the mood swings of the vpn at my workplace (and it is having a hostile mood at the moment).

I figuered out a loop on my own, without using java snippet at all. I guess it’s more or less the same as the one you posted :slight_smile:


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