Variable Expression Split String

I am playing around with the widget configuration and was trying to use the Variable Expression (Learning) but im stuck on combining the output of a split with a string.

v=variable(“value-selection”)
vsplit = split(string(v),“\”)
vsplit_string = arrayToString(arraySubset(vsplit, 0, 1))
join(“jdbc:sqlserver://”,string(vsplit_string))

the output shows the server combined with the JDBC string but the server has {} around it meaning its not converting it to a string but stays as an array. e.g. jdbc:sqlserver://{ServerAddress}

When feeding this into a server connection it fails.

Hi @kb2010 and welcome to the KNIME community!

A quick tip before getting started, when pasting in code here on the forum, it is useful to highlight it and use the “preformatted text” button so that the forum doesn’t try to convert quotes into smart-quotes etc,

i.e.

then it makes it easier for people to copy/paste your code and we can also be sure that what we are reading is in fact what your code actually says. (Mind you, it can have a bit of a mind of its own when it comes to adding colour to it! :wink:

For example, I’m assuming that your code at line 2 actually says this:

vsplit = split(string(v),"\\")

with two backslashes, otherwise you would be getting errors from the Variable Expression parser, e.g.

image

So I’m further assuming your full code is actually as follows:

v=variable("value-selection")
vsplit = split(string(v),"\\")
vsplit_string = arrayToString(arraySubset(vsplit, 0, 1))
join("jdbc:sqlserver://",string(vsplit_string))

I’m guessing that your input value-selection variable has a number of values separated by \

What you appear to be doing here is taking a string that is delimited by \, turning it into an array which is fair enough. What you need then is the syntax for referencing the elements of that array.

You can do this using the [subscript] syntax where the first element is subscript [0], the next is subscript [1] and so on. So to take the first element from the array “vsplit”, you can just use vsplit[0] like this:

v = variable("value-selection")
vsplit = split(string(v),"\\")
join("jdbc:sqlserver://",vsplit[0])

In terms of the use of the arraySubset function, this returns an array which is a subset of the original array rather than a specific element of the array. You could use it, but it wouldn’t give any gains, as you would still have to then reference the required element from it using the subscript notation, and all you have done is create an extra array object for no real purpose:

e.g. this might then look like this, but I’d suggest you’d just want to use the previous code example

v=variable("value-selection")
vsplit = split(string(v),"\\")
vsplit_string = arraySubset(vsplit, 0, 1)
join("jdbc:sqlserver://",vsplit_string[0])

Does that help get you what you need?

3 Likes

Thank you. I apologies it was my first time posting I didn’t think about using the preformatting function to ensure formatting was correct :frowning: I will try to make sure I do that in the future. :slight_smile:

Regarding your solution you were spot on. I probably should have tried to use that syntax because its how we would normally access array elements in other programming languages. Thank you for your help takbb :slight_smile:

2 Likes

Not a problem @kb2010. Glad that’s helped and welcome again!

Oh and thank you for marking it as the solution. That helps other people too when they search in the future and also let’s people know if a question still needs answering.

1 Like

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