Skip to main content
Participating Frequently
Jun 24, 2024 7:28 AM UTC
Question

previous iteration in foreach

Good afternoon,

I am trying to compare in a foreach loop a data with the previous iteration, but I am not able to access this iteration, is there any way?

I need to sort a list, but if there are two equal values ​​they have the same order,
with the array
{
name:"aaaaa", valor:2
name:"bbbbb", valor:10
name:"ccccc", valor:10
name:"ddddd", valor:10
name:"eeeee", valor:45
}
get as a result
{
name:"aaaaa", pos:1
name:"bbbbb", pos:2
name:"ccccc", pos:2
name:"ddddd", pos:2
name:"eeeee", pos:3
}

I am trying to save the pos outside of the foreach in a local variable, or recover the value saved in pos of the previous item

Thank you very much

13 replies

pedrob2551
Participating Frequently
Jun 24, 2024 7:37 AM UTC

Can you give more information? You need a specific iteration, a varying one or all of them?

Participating Frequently
Jun 24, 2024 7:49 AM UTC
I need to sort a list, but if there are two equal values ​​they have the same order, 
with the array
{
name:"aaaaa", valor:2
name:"bbbbb", valor:10
name:"ccccc", valor:10
name:"ddddd", valor:10
name:"eeeee", valor:45
}
get as a result
{
name:"aaaaa", pos:1
name:"bbbbb", pos:2
name:"ccccc", pos:2
name:"ddddd", pos:2
name:"eeeee", pos:3
}

I am trying to save the pos outside of the foreach in a local variable, or recover the value saved in pos of the previous item
pedrob2551
Participating Frequently
Jun 24, 2024 8:38 AM UTC

As yogip3177 is sort of hinting, you can use a!foreach functions like fv!isFirst, fv!isLast, fv!item and even fv!item-1 (-x) to move around the foreach iterations, more then that you can have local variables inside local variables like shown in his code snippet, the only thing is that local variables can only ba accessed inside it's own function so the variables inside the second localVariable function won't be accessed on the 'father' local variable function

Brainy
Jun 24, 2024 7:48 AM UTC

Not sure about the exact requirements. The following code snippet might be helpful:

a!localVariables(
  local!data: {"A","B","C","D"},
  a!forEach(
    items: local!data,
    expression: a!localVariables(
      local!previousData: if(not(fv!isFirst), index(local!data, fv!index-1), null),
      a!map(data:fv!item, previous: local!previousData)
    )
  )
)

Participating Frequently
Jun 24, 2024 7:51 AM UTC

I need to sort a list, but if there are two equal values ​​they have the same order,
with the array
{
name:"aaaaa", valor:2
name:"bbbbb", valor:10
name:"ccccc", valor:10
name:"ddddd", valor:10
name:"eeeee", valor:45
}
get as a result
{
name:"aaaaa", pos:1
name:"bbbbb", pos:2
name:"ccccc", pos:2
name:"ddddd", pos:2
name:"eeeee", pos:3
}

I am trying to save the pos outside of the foreach in a local variable, or recover the value saved in pos of the previous item

davidj137213
Brainy
Jun 24, 2024 8:26 AM UTC

I did not understand what you are trying to implemented... Could you give us more information? What it would the expected output?

Inspiring
Jun 24, 2024 10:33 AM UTC

This will fulfil your requirement if I understood your query correctly

a!localVariables(
  local!data: {
    a!map(
      name: "aaaaa",
      valor: 1
    ),
    a!map(
      name: "bbbbb",
      valor: 10
    ),
    a!map(
      name: "ccccc",
      valor: 10
    ),
    a!map(
      name: "ddddd",
      valor: 10
    ),
    a!map(
      name: "eeeee",
      valor: 45
    )
  },
  local!uniqueValors: union(local!data.valor, tointeger({})),
  local!valorIndices: createdictionary(
    local!uniqueValors,
    enumerate(length(local!uniqueValors))+1
  ),
  a!forEach(
    items: local!data,
    expression: a!map(
      name: fv!item.name,
      pos: property(local!valorIndices, tostring(fv!item.valor), null)
    )
  )
)

pedrob2551
Participating Frequently
Jun 24, 2024 10:41 AM UTC

The problem with that, and please correct me if I'm wrong, is that it will only work if the 'valor' values are ordered.
In adition, never seen a 'Bean' Appian type as shown in the property function that you are using!!!!!!!!

Inspiring
Jun 24, 2024 10:43 AM UTC

yes for this the values should be ordered

konduruc733182
Inspiring
Jun 24, 2024 5:22 PM UTC

This might be using a bit more iterations, but gives the exact out put you expect.

a!localVariables(
  local!values: {
    { name: "aaaaa", valor: 2 },
    { name: "ddddd", valor: 10 },
    { name: "ccccc", valor: 10 },
    { name: "bbbbb", valor: 10 },
    { name: "eeeee", valor: 45 }
  },
  local!uniqueValor: union(local!values.valor, local!values.valor),
  local!map: a!forEach(
    items: local!uniqueValor,
    expression: todatasubset(
      arrayToPage: a!forEach(
        items: wherecontains(fv!item, tointeger(local!values.valor)),
        expression: a!map(
          name: index(local!values.name, fv!item, null),
          valor: index(local!values.valor, fv!item, null)
        )
      ),
      pagingConfiguration: a!pagingInfo(
        startIndex: 1,
        batchSize: 5000,
        sort: a!sortInfo(field:"name",ascending: true)
      )
    ).data
  ),
  todatasubset(
    arrayToPage: a!flatten(local!map),
    pagingConfiguration: a!pagingInfo(
      startIndex: 1,
      batchSize: 5000,
      sort: a!sortInfo(
        field: "valor",
        ascending: true
      )
    )
  ).data
)