value by ranges

A Score Level 1

I need an expression which finds corresponding value based on ranges:

Input:
x
{v1,v2,...,vn+1}
{r1,r2,...,rn}

Result:
if (x<=r1) then v1
else if (x<=r2) then v2
....
else if (x<=rn) then vn
else vn+1

Thanks

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Assuming that the list of ranges is sorted, the following code should do this. I use the list comprehension feature of operators to compare x to all ranges, the use where() to get the indexes of matches, pick the first match and the pick the value for that matching index.

    a!localVariables(
      local!x: 4,
      local!values: {23, 5, 468, 123, 42, 500},
      local!ranges: {1, 2, 3, 4, 5},
      index(
        local!values,
        index(where(local!x <= local!ranges), 1, count(local!values)),
        0
      )
    )

Reply
  • 0
    Certified Lead Developer

    Assuming that the list of ranges is sorted, the following code should do this. I use the list comprehension feature of operators to compare x to all ranges, the use where() to get the indexes of matches, pick the first match and the pick the value for that matching index.

    a!localVariables(
      local!x: 4,
      local!values: {23, 5, 468, 123, 42, 500},
      local!ranges: {1, 2, 3, 4, 5},
      index(
        local!values,
        index(where(local!x <= local!ranges), 1, count(local!values)),
        0
      )
    )

Children
No Data