If statement in new node expresion

Hi all

I am sorry if this sounds very basic question. please note i don’t have coding background hence often find difficulty in writing even the simplest code.

I have 10 fields where the entries are as follows:

L9_layer contains either “B9” or “No B9”
L7_layer contains either “B7” or “No B7”
L2_layer contains either “B2” or “No B2”
L3_layer contains either “B3” or “No B3”
L23_layer contains either “B23” or “No B23”
n7_layer contains either “n7” or “No n7”
n23_layer contains either “n23” or “No n23”
n70_layer contains either “n70” or “No n70”

in the new field called layer_config, i want blank if all the layers has no entries meaning all the layers have “No_xxx” corresponding values but if any of these have other than “No_xxx” i want to combine them. Like if L9 and L7 has B9 & B7, it should return B9/B7 and if B9 has “No B9”, then it should be “B7”

this means it will combine all the values together separated by “/” if the values are other than “No” in it.

i was trying the following expression

if(
$[“L9_layer”] = "No B9,
if($[“L7_layer”] = “No B7”, “”, $[“L7_layer”]),
if($[“L7_layer”] = “No B28”, $[“L9_layer”], join($[“L9_layer”], “/”, $[“L700_layer”]))
)
but the value o am getting is “/B7B9” instead of B7/B9

Can any one help what wrong i am doing?

thank you very much in advance

@Mobihashim01 if then else in the new Expression node is simple and sometimes not immediately obvious:

3 Likes

Hi @Mobihashim01 , it’s tricky to debug code directly on the forum without access to sample data, and also because the forum software modifies the uploaded source code unless you highlight it and use the “preformatted text” button, or else wrap it with ` symbols

From what you’ve pasted above, with the “smart quotes” removed and the double-quotes returned, plus a small correction for the missing closing double-quote after "No B9 , I believe your code is as follows:

if(
  $["L9_layer"] = "No B9",
  if($["L7_layer"] = "No B7", "", $["L7_layer"]),
  if($["L7_layer"] = "No B28", $["L9_layer"], join($["L9_layer"], "/", $["L700_layer"]))
)

and the following screenshot shows the problem you are encountering:

This is caused not by any logic error on your part - you’ve got the IF… else syntax correct - but because you got the join syntax wrong.

In the Expressions node, the join syntax is actually the same as the joinSep syntax in the old String Manipulation node, and is one of those counter-intuitive inconsistencies that you need to be aware of; the first parameter passed to join (in the new Expressions node) is actually the separator to be used in between all the other values.

So what is happening is it is return “/” and $["L700_layer"] separated by $["L9_layer"].

image

What you actually want to say is

join("/", $["L9_layer"], $["L700_layer"])

and then it will work.

If that syntax seems a little convoluted, you can alternatively use the + operator which will concatenate strings too:

$["L9_layer"]+ "/"+ $["L700_layer"]

3 Likes

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