Replace a null or empty string with other values using condtional Statment

HI guys,
i can’t figure out what i s wrong with my conditional statement in this design script code.
The goal is to replace the value of a parameter of null or of an empty string with “NA”, except when the parameter is “Length” then i would replace with “0.00”.

any ideas why this code is not working?
thanks,

You have multiple ? in your If-statement.
I would guess that you’ll have to replace the first ? with || (Or-statement).

Also, I would highly suggest splitting 2 if-statements in two parts of the scripts instead of trying to do it in one, to prevent confusion and to make it easier for yourself. (And when you need to edit the script later on)

but i am pretty sure that you can nest conditional statements… though i cannot get a reference for you now…
i have seen a structure like this:

pv0 != "" &&  pv0!=null? pv0:
                 pa=="Length"?"0.00":"NA";

i guess it makes it more readable, but it is only 2 conditions so i did not bother with that.
how would you have written it?

As far as I see ‘pa’ is a list, not a string.

Also this:

pv0 != "" && pv0!=null

Is programmatically in the wrong order. If it is not an empty string then it never can be null. Well, it can but then you have gotten an error anyway. I think the function GetParameterValueByName always returns a string, but if not you should use this:

pv0 != null && pv0 != ""

The problem is that Dynamo is not used to dealing with different list lenghts in IF statements.

You should use List.Cycle your list pa, using List.Count at level 2 in your pv0.

I also assumed that you used List.Transpose on GetParameterValueByName or replaced levels on 2 and 1. That way you get all values for single parameter in 1 sublist.

So just so i understand this …there is no problem with the structure of my conditional statement… it’s just that i happened to have used it in the wrong scenario, i.e. lists of different sizes?

would i have the same issue if i wrote it in if…else instead?
or should i just do it in a python?

I mean the list structure is just a Dynamo thing, and you can work around it, my screenshot is just 1 solution. You can use List.Cycle in a different way (just different levels) and it will work. You can also go in Python and get the same result.

What i want to say is you just need to work around different list lengths.

Hi @fawzimasri7137 ,

I am currently learning design script. Here are a couple of concepts for you to investigate: imperative programming and associative programming. I answer your question using both methods (associative/imperative):


1 Like

@francolaca thanks for the input.
I ended up using the imperative method, since i do not like so many nodes-they tend to clutter /crowd-up the place that it makes it hard to see the whole code.
in any case, i wonder if that list.Cycle should be incorporated as lacing on code blocks … since it’s a similar problem… but i don’t know enough about this to make a suggestion.