if(starts_with($[“备注”], “test”))
once I write this express in the “Expression” node , it will be wrong ! please tell me what is the problem?
hi @lanfengye
starts_with is no proper function in the column expression node.
in case you want to refer to a column with $[“备注”] select the column via “+ column” and replace your pattern
try this
pattern = "ab"
if(left("abc",length(pattern))==pattern) ...
hope that helps, tommy
thanks a lot,tommy!
but I don’t understand it yet…
like now follow:
regex_extract($[“选购商品”], “[a-zA-Z]{2,}”)
and why it can’t work ?
hi @lanfengye
you might use the function
regexReplace(str,regex,replaceStr).
Each argument should start and end with " or refer to a column.
$[“选购商品”] might refer to a column. However, you should insert it via the + column option in Column Expression Node.
best tommy
Hi @tommy, I think that @lanfengye is referring to the new Expressions node in KNIME 5.3, rather than the Column Expressions node, hence the confusion.
Hi @lanfengye , welcome to the KNIME forum. This is an odd one that having given it a play, I can see why it feels that something is amiss.
The syntax for the if()
statement is if(condition, result-if-true, result-if-false)
So on that basis, your statement ought to be something like this:
if(starts_with($["备注"],"test"),"Does start with 'test'", "Does not start with 'test'")
but when I tried that, I was surprised by the error it gave:
What I found through trial and error was that I had to wrap the string( ) function around the column name, even though the column is a String
if(starts_with(string($["备注"]),"test"),"Does start with 'test'", "Does not start with 'test'")
This then worked as you can see here.
I did wonder if this was because of the non-western characters in the column name, but replacing it with my own “Note” column resulted in the same issue:
Unfortunately the examples in the Help for the IF statement are not great, as they appear not to show the correct syntax for column names etc, so give no clues about what the problem is.
Hopefully though using the above string( ) function you can make some progress!
edit:
I just installed K-AI to see if it would have given any clues. Unfortunately K-AI is also unaware of the need to use the string() function for this, which does makes me wonder if this is a bug, or we are missing something “obvious” :
Hi @takbb,
The confusion is related to missing values that need to be handled explicitly in the new KNIME Expression Language. The value of $["col"]
can be MISSING
. Therefore, the return value of starts_with($["col"], "test")
is either TRUE
, FALSE
, or MISSING
. However, the if
function requires the first argument to be either TRUE
or FALSE
.
The easiest way to get rid of MISSING
is to use the Missing coalescing operator ??
:
if(
starts_with($["col"], "test") ?? FALSE,
"Does start with 'test'",
"Does not start with 'test'"
)
Note that string($["col"])
returns the string "MISSING"
(note the quotes) if the cell value is missing. Therefore, the first argument of starts_with(string($["col"]), "test")
cannot be MISSING
and the function returns either TRUE
or FALSE
(as required by the if
function).
thank you @bwilhelm for the explanation.
I hadn’t spotted the ?? (coalesce) operator, and now see it is mentioned on the node documentation rather than the help info within the expressions configuration. At least I haven’t yet found it mentioned within the config documentation.
The other thing I now realise hadn’t fully understood was the error message:
I had read this as “not applicable to the arguments [BOOLEAN, STRING, STRING]” and was puzzled because from what I could see “BOOLEAN, STRING, STRING” is exactly the arguments I had expected to be applicable.
Now I realise (assume) that the subtle “?” in BOOLEAN? means BOOLEAN|MISSING.
For people coming new to this node, as I guess most of us still are, the real meaning BOOLEAN? doesn’t feel immediately apparent, but now I know, I shall bookmark this thread, because I have a feeling that I may be pointing people at it in the future
thanks again!
Thank you for your feedback. It’s helpful to understand the challenges of working with the node.
We have already implemented a few improvements (available in the nightly build):
- Error messages now specify which argument has the incorrect type and how it compares to the expected type.
- We no longer use
?
to indicate optional values. Instead, we explicitly useBOOLEAN | MISSING
.
In addition, we should make the missing coalescing operator easier to discover and improve the handling of missing values. Our goal is to ensure that it’s always intuitive and doesn’t require a lengthy explanation in a forum thread .
Thanks for the update @bwilhelm.
These sounds like some great usability improvements, and always good for the community to see a direct flow of how questions and issues raised on the forum can feed directly into the ongoing improvements.