3/23

Split Array Into Chunks

Easy

Given an array of integers and an integer N, return a 2-D array where each sub-array contains at most N consecutive elements from the original array.

The elements must remain in their original order.


Input:

ri!arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
ri!N = 2


Output:

{
{1, 2},
{3, 4},
{5, 6},
{7, 8},
{9, 10}
}


Note: The last sub-array may contain fewer than N elements when the array length is not a multiple of N.

ri! Rule Inputs

ri!arrList of Number (Integer)
{
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10
}
ri!NNumber (Integer)
2

Example 1 — Case 1

Input: ri!arr
{
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10
}
Input: ri!N
2
Output:
{
  {1, 2},
  {3, 4},
  {5, 6},
  {7, 8},
  {9, 10}
}

Example 2 — Case 2

Input: ri!arr
{
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10
}
Input: ri!N
3
Output:
{
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9},
  {10}
}

Example 3 — Case 3

Input: ri!arr
{
  11,
  22,
  34,
  56,
  78,
  9,
  10,
  6,
  18,
  19,
  90
}
Input: ri!N
4
Output:
{
  {11, 22, 34, 56},
  {78, 9, 10, 6},
  {18, 19, 90}
}

Appian Expression Tips

  • • Use ri!variableName for rule inputs
  • • Use a!localVariables(local!x: …, body) for locals
  • • Use a!forEach(items: …, expression: fv!item) to iterate
  • • Use where() / reject(fn!isnull, list) to filter