How to generate all integers in between? eg: 2 TO 5 becomes 2|3|4|5

Hi, is there a node I can use to generate and concatenate all integers inbetween a pair of numbers?
Say for example I have row values of
2 TO 5
and
-2 TO 3
I have used a Cell Splitter to convert these to 2 columns so I have FROM and TO values. How can I fill in the inbetweens with a delimiter so the outputs are
2|3|4|5
and
-2|-1|0|1|2
?
thanks,
James

Hi,

Here is one way to do this … use of python script node (you must have python installed) and groupby node.

RangeFromXtoY.knwf (16.6 KB)

Instead of aggregation method list you can use concatenate and use “|” as a delimiter …

Hope it helps you in some way

3 Likes

Hi James,

I was also able to accomplish this using a counting loop that created empty rows, filled those rows with a row ID integer, added that integer to the min value, and then concatenated that value with “|”.

2 Likes

And just to add to the set, here is another aternative that doesn’t use python or a loop… :slight_smile:

Generate Sequence.knwf (24.6 KB)

As is so often the case, KNIME allows you to be spoilt for choice!

8 Likes

Thank you, this works wonderful and even has a catch for direction! Bravo :grinning:

1 Like

Thank you. I didn’t have python set up, although I’m sure I will need it soon

thanks for the suggestion. What are the red connections between nodes?

The red connections carry Flow Variables only, without data. Flow Variables are exactly that: Variables that hold information about the workflow. You can use them to carry meta-information about the table, remote control nodes, monitor loop iterations and much more.
You can read more about them here:
KNIME Flow Control Guide

Since takbb has already provided a neat loop free solution, I’ll resort to cheating and use the Java Snippet Node:

int i, dir=1;
int start, end, delta;
String buffer;

buffer = c_RANGE.split(" TO ");
start = Integer.valueOf(buffer[0]);
end = Integer.valueOf(buffer[1]);
delta = Math.abs(end - start);

if (start > end) dir = -1;

out_gen_seq = “”;
for (i=0; i<=delta; i++) {
if (i > 0) out_gen_seq += “|”;
out_gen_seq += (start + i * dir);
}

4 Likes

haha, something for everyone! :grinning:
This is our first experience of using KNIME, but we’re super pleased that we found it. We have lots to learn, but much of it feels like familiar territory for us, which is great.
I only wish we knew about this years ago!

3 Likes

Nice one @Thyme! :wink:

As the forum software has unfortunately used some of your java code for markup :wink: I thought I’d repost it here, with a screenshot of where it goes in the snippet for context, so that others less familiar with Java can make use of it.

// Enter your code here:
int i, dir=1;
int start, end, delta;
String buffer[];

buffer = c_RANGE.split(" TO ");
start = Integer.valueOf(buffer[0]);
end = Integer.valueOf(buffer[1]);
delta = Math.abs(end - start);

if (start > end) dir = -1;

out_gen_seq = "";
for (i=0; i<=delta; i++) {
if (i > 0) out_gen_seq += "|";
out_gen_seq += (start + i * dir);
}

Note for others… to make the code render correctly, I pasted in the java code (correct version rather than “marked-up” version), then highlighted it and pressed the “preformatted text” button which you can see here:
image

Thanks again for your java code @Thyme ! Just waiting for somebody to provide the R solution… @ScottF ?? :wink: and the Column Expressions node is I believe still up for grabs! :slight_smile:

Thank you mr @takbb
Best regards

1 Like

Hi @amirreza992 , glad you liked it. Welcome to the forum! :slight_smile:

Oh, I didn’t know that’s how to put in code snippets. My bad. Thanks for fixing it for me.

I should note that this snippet (as all my code) is not safe. There’s no error catching, if the input is bad, it will fail without grace. That is sort of intentional, my first language was C and it shows :innocent:

1 Like

I was bored and Node Golfed this further.

  • I upgraded the Java Snippet so it handles inputs that have a single number only (i.e. “1”).
  • Never used Column Expressions before, and probably never will again. It’s nothing more than a port to JavaScript.
  • It’s also my first thyme using R Snippet Node. I use R extensively in visualising my data though. Took quite a while to do it vectorised, because I never had to do that before.
  • No snake, because I don’t have it installed on this machine.

Generate Sequence - Node Golf Version
Generate Sequence - Node Golf Version.knwf (22.7 KB)

3 Likes

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