Skip to main content
medap0001
January 16, 2023
Question

I need to get that by using the for each function

  • January 16, 2023
  • 5 replies
  • 0 views

Hi all,

{1,2,3,8,9,9,9,9,9,80,90,70}, {56,76,54,32}, {20,23,45,54,32,54}, {1,2,3,4,5,6,7}, {11,12,13,14,15,16,17,18}

Please suggest how do this using foreach function get output like this,


length of array 1 is 12
length of array 2 is 4
length of array 3 is 6
length of array 4 is 7
length of array 5 is 8


Thank You In Advance

5 replies

stewart.burchell
January 16, 2023
  1. Here's the approach I would take (to help you work it out for yourself):Treat each array as a single item in a new array (that is, your new array is a "list of lists")
  2. iterate over that new array using a!forEach()
  3. for each item in the array use the function that tells you how many items are in an array
Inspiring
January 16, 2023

I tried this but appian doesn't consider multiple arrays in a single array as list of lists. Appian is considering it as a single list

Inspiring
January 16, 2023

Appian will take this as a single list of integers. To achieve what you want, the best solution I can think of is making a map for all individual arrays and then iterating over it using the keys.

a!localVariables(
  local!arrays: a!map(
    1: {1,2,3,8,9,9,9,9,9,80,90,70}, 
    2: {56,76,54,32}, 
    3: {20,23,45,54,32,54}, 
    4: {1,2,3,4,5,6,7}, 
    5: {11,12,13,14,15,16,17,18}
  ),
  a!forEach(
    items: a!keys(local!arrays),
    expression: "Length of array " & fv!index & " is " & length(local!arrays[fv!item])
  )
)

Inspiring
January 17, 2023

One more solution I found to this problem is using merge() function. The code is attached below.

a!localVariables(
  local!arr: merge(
    { 1, 2, 3, 8, 9, 9, 9, 9, 9, 80, 90, 70 },
    { 56, 76, 54, 32 },
    { 20, 23, 45, 54, 32, 54 },
    { 1, 2, 3, 4, 5, 6, 7 },
    { 11, 12, 13, 14, 15, 16, 17, 18 }
  ),
  local!size: {
    a!forEach(
      items: local!arr[1],
      expression: a!localVariables(
        local!temp: fv!index,
        a!forEach(
          items: enumerate(length(local!arr)) + 1,
          expression: if(
            a!isNullOrEmpty(local!arr[fv!item][local!temp]),
            0,
            1
          )
        )
      )
    )
  },
  a!forEach(
    items: local!size,
    expression: "length of array " & fv!index & " is " & sum(fv!item)
  )
)