Replace 0 values by previous positive value in list

Hi everyone,

I can’t figure how to solve an “apparently’“ simple problem on lists.

I’m dealing with a list of values following this pattern :

{0}
[400;0;0;0;0;120;0;0;30;0]
{1}
[650;0;0;320;0;0;0;0;0;0;0;110;0]

I’d like to replace the 0 values by the previous positive value. Following this simple rule, I expect :

{0}
[400;400;400;400;400;120;120;120;30;30]
{1}
[650;650;650;320;320;320;320;320;
320;320;320;110;110]

As you could have seen, number of items in branches may differ.

I really hope someone could help me :slight_smile:

Thanks,
Thib

Is it always that structure? Single digits separated by zeros? Never two non-zero numbers next to each other?

Does it matter if it were?

Not sure till I try, but it’s good to have clear specification of a problem :slight_smile:

It’s just a falling edge detector.

but we are sure that the first item is not 0 ?

I would assume, but in that case make <“null”>

replace0withprev_jvs01.gh (13.7 KB)

2 Likes

you still have to define:
what should happen if the list starts with 0 ?

[0;0;400;0;0;0;0;120;0;0;30;0]

and yes, solve it by a simple script as @Jakob_v_Schirmeister showed.

Like I said, you then start replacement with a null.

Well, just for fun, here’s a way without a script :slight_smile:

replace following zeros TN.gh (9.0 KB)

2 Likes


Replace 0 values by previous positive value in list VR 02.gh (16.9 KB)

1 Like

:smiling_face_with_tear: