Replace zero from an Integer array

Hi All,

I need to replace zero with next value in array.

ex: {1,2,3,0,0,10,15,0,14,0} ; then result should be {1,2,3,10,10,10,15,14,14,0}

Thanks

  Discussion posts and replies are publicly visible

Parents
  • a!localVariables(
      local!values: {1,2,3,0,0,10,15,0,14,0},
      a!forEach(
        items: local!values,
        expression: if(
          fv!item = 0,
          index(
            /* Only take values after the current one */
            ldrop(local!values, fv!index - 1),
            /* Find the first not being zero */
            /* where() evaluates zero as false and >0 as true */
            index(where(ldrop(local!values, fv!index - 1)), 1, 0),
            fv!item
          ),
          fv!item
        )
      )
    )

    This should do it. May I ask what the use case is?

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

    Out of curiosity, why not use something more simple like, "if(fv!item = 0, index(local!values, fv!index+1, 0), fv!item)"?  Granted, I haven't tested either way, just my quick-glance impression.  I don't immediately see why we need to involve ldrop(), for example.

    Edit: i missed the requirement that if the next value is zero then it should forward-seek until it finds the first nonzero value.  I'll just go ahead and retract my objection ;-)

Reply
  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

    Out of curiosity, why not use something more simple like, "if(fv!item = 0, index(local!values, fv!index+1, 0), fv!item)"?  Granted, I haven't tested either way, just my quick-glance impression.  I don't immediately see why we need to involve ldrop(), for example.

    Edit: i missed the requirement that if the next value is zero then it should forward-seek until it finds the first nonzero value.  I'll just go ahead and retract my objection ;-)

Children
No Data