Code Block Not Working Properly

I wrote a code block which is supposed to receive an object X and if the X[0] == “True”, return the first object on the list X[1][0], else return the object itself X[1]. It works when feed it with a single object but when I feed it with a list it doesn’t work properly.
Is there something I’m doing wrong?

Feed of single object

Feed of list

This is how if statements work in a code block, as they don’t have any means of replication guides or lacing control. Therefore if you feed a mismatched list of booleans, true values, or false values you will always get shortest lacing applied.

A viable solution would be something like this:

indx = test ? 0 : 1;
result = [trueList, falseList][indx];

Modifying that to what I think you want, it would be:

indx = a[0] ? 0 : 1 ; 
result = [ a[1], a[1][0] ][indx];

A beater solution might be DSCore.List.FirstItem(a[1]@L1<1>);

PS: if something solves a question for you don’t forget to mark it as a solution (saw a thread on your custom node earlier today).

3 Likes

Thanks for the response. Any chance for a short example why it is needed to assign the condition and results to the ‘indx’ and ‘result’ var and how to use it?
I’m sorry, just not deeply familiar with the designScript. When I looked online I didn’t really find any good source that wrote code that way unless it was in an imperative block…

Don’t worry I’ll mark the solution when I get to it. I managed to solve the selection of level from element which I will post but this node is a piece of it so I want to get it done right :slight_smile:
Thanks for all the help!

So when I change my code to the following it manages to return the single member (as supposed to the previous code) but it still gives me the whole list as supposed to the first member of the list. Any way to single that out?
Can you also expand on why the condition didn’t respond well to condition “X[0] == ‘True’ ? 1 : 0” but it does to “X[0]’ ? 1 : 0” ?

IsList -> False

IsList -> True

I don’t have plans on booting up the laptop today, but if I do I’ll give it a shot. Fortunately you can find examples of this if you search ‘design script if’ on the forum: Workaround with If statement and empty list - #7 by JacobSmall

I think you may have inverses the input variables in your example - there is an ‘x’ instead of an ‘a’ or vice versa. Not sure if that will resolve though.

My thought on the list.first item was to just use that line of design script, or the List.FirsItem node. Not sure that an if statement is needed.

Hi,
a workaround using Level (@L1)

listworkaroubd2

Another solution, by converting the elements to a list (get the first index)

listworkaroubd

2 Likes

My previously proposed method. Should be a bit quicker than flattening the list as it won’t need to pack the list into memory. My previously typed example used @L1 instead of @L2 though… sorry for the confusion.

2 Likes

This worked! The syntax isn’t the clearest but I’ll take it :slight_smile:

1 Like