Converting text to boolean


#1

I have a list of YES/NO text and I would like to convert these to their boolean representation. I first convert the YES entries in my list to true and dynamo does this just fine as shown below. However, I do not understand why the last ReplaceByCondition node returns the same list from the prvious step.

Also, are the true entries in the first ReplaceByCondition node truly boolean values? I ask this because I modified my graph to use the != node and this was my result.


List consisting of both values and booleans from excel to revit
#2

You could use a code block?


#3

What are you unsure of? The result is correct. You want to replace anything that doesn’t equal true with false and it only changes the results that weren’t already true.

The (purple) true and false objects are actual booleans as you can see here:


#4

Yes, your solution works just fine and I will probably end up using this. I would still like to know why my graph isn’t working :frowning:


#5

@Andaroo try running your list through a String.TrimTrailingWhitespace node before passing it through your ReplaceByCondition node… It’s possible the “NO” has an extra space or two after it, but unlikely if @Ewan_Opie’s code block worked :thinking:


#6

Strangely, mine fails (passes?) for all items.

If I change the condition to use non-booleans it works. I’m guessing the node is getting confused because it’s checking the condition but the input item is already a true boolean so it uses the replacement value anyway.

EDIT:
Seems like existing true values break conditional statements? Is this new? I don’t think I’ve ever seen this before.


#7

I’m seeing the same thing on my end @Nick_Boyts - oddly I can’t even replicate @Andaroo 's != true outcome either; only the CodeBlock method returns correctly.


#8

I can’t replicate it either. CodeBlock only seems to work if both conditions are run in the same line. Breaking the conditions out into separate checks gives the same incorrect output.

EDIT:
Order matters, but item value vs conditional value doesn’t. Any true value seems to break the function.
image
image


#9

Posted to GitHub.


#10

Seems to me like you’re over-complicating your life a bit.

If you truly have only two cases (yes or no), all you’d need to do is the below:
image

And if you suspect there could be other conditions (more than two states), then you could go for a nested if and either preserve the unknown state:

or replace it with whatever you think is appropriate:


#11

I have found that Dynamo generally hates performing analysis on mixed data types. By converting only some of the data to a Boolean (true/false) you create this mixed data set. This is similar to when a dataset contains null values or an empty list. Sometimes I have been able to get around this with lacing and level manipulation, but that is dicey to say the least. As @Dimitar_Venkov pointed out, simplifying your code to perform the function on the data while it is a single type is certainly the simplest method here.

Another good test here is to ensure the correct data is being fed in before you proceed. Run the data into a List.UniqueItems node and then into a few other tests or List removal functions to produce a single Boolean for ‘all data is matching types’. That Boolean can then cause Dynamo to throw up a popup to say something like ‘The given dataset has unsupported values. Please review the dataset and try again.’


#12

@Nick_Boyts @awilliams @Andaroo it is worth noting that there are certain type conversion rules that are built in to the language in Dynamo. When strings are converted into boolean values, the following rules apply:

  1. An empty string is equivalent to a Boolean ‘false’
  2. A non-empty string is equivalent to a Boolean ‘true’

I hope this answers some of the confusion above.