Skip to main content
December 2, 2021
Question

How to apply a condition of a list of numbers

  • December 2, 2021
  • 5 replies
  • 2 views

I have this local variable

local!variable:
{2017,2018,2019,2020}

local!variable2:
a!forEach(
   items: local!variable,
   expression: if(fv!item<2018,"PRE-YEAR",fv!item)
 ),

I wanted a result like:

{"PRE-YEAR",2018,2019,2020}

Error:

Please help me 

    5 replies

    stewart.burchell
    December 2, 2021

    I do not see the error you are sharing:

    a!localVariables(
      local!variable: { 2017, 2018, 2019, 2020 },
      local!variable2: a!forEach(
        items: local!variable,
        expression: if(fv!item < 2018, "PRE-YEAR", fv!item)
      ),
      local!variable2
    )

    ...results in:

    December 2, 2021

    This is the initial data that is list of dictionary and i Need result in list of numbers

    stewart.burchell
    December 2, 2021
    {"PRE-YEAR",2018,2019,2020}

    You said this is what your result needed to be like...by definition it cannot be a list of numbers since your code is outputting "PRE-YEAR" when the item in your list is < 2018.

    stewart.burchell
    December 2, 2021

    ...just trying to understand your response...are you saying that the input to your rule is a list of Dictionary, but that you want to examine the value of each item as per your original code...that is, something like this:

    a!localVariables(
      local!variable: { 
        { EOMYear: 2017 },
        { EOMYear: 2018 },
        { EOMYear: 2019 },
        { EOMYear: 2020 },
        { EOMYear: 2021 }
      },
      a!forEach(
        items: local!variable,
        expression: if(
          fn!tointeger(fv!item.EOMYear) < 2018,
          "PRE-YEAR",
          fv!item.EOMYear
        )
      )
    )

    ...which results in:

    paoloc0002
    December 2, 2021

    Hello friend, 

    You are very close to your solution! 

    the error you are receiving is due to the comparison operand. You should just be able to do: if( to integer(fv!item) < 2018, "pre", fv!item)

    Furthermore, based on your output, it seems that you are doing this in an expression rule, so you'll have to return the value on it's own or as the name of the variable, please see below the code snippet: 

    -- Start

    local!variable: {2017, 2018, 2019, 2020}

    local!variable2: a!forEach(
        items: local!variable,
        expression: if( tointeger(fv!item) < 2018, "PRE-YEAR", fv!item)
    ),

    /*return */ 

    local!variable2

    -- End

    If this is the value you need to return, you can go without the local variable 2 and just explicitly do the a!forEach at the end of the rule, but if it's not, that should resolve your issue! Best wishes.