Question on converting array into sub-arrays

Given an array
{"a","b","c","d","e","f", "g","h","i"}

Is there a way to convert it to an equal size sub-arrays as follows (say by specifying 3 in this case):
{{"a","b","c"}, {"d","e","f"}, {"g","h","i"}}

OriginalPostID-227719

OriginalPostID-227719

  Discussion posts and replies are publicly visible

  • I don't believe there's an existing function for this. You would have to use a combination of looping functions: forum.appian.com/.../Looping_Functions.html and array functions:
    forum.appian.com/.../Array_Functions.html
  • It will be a bit tricky constructing the sub-arrays from the main array using OOTB functions.

    My goal is to construct the following grid from the main grid:
    ---------------------------------
    column1 | column2 | column3 |
    ---------------------------------
    a |b |c |
    ---------------------------------
    d |e |f |
    ---------------------------------
    g |h |i |
    ---------------------------------

    Any good ideas how to acheive that?
  • To achieve the grid you want, you will be looking for arrays in this format instead, so each can be used as a column: {a,d,g},{b,e,h},{c,f,i}. This function below will handle a dynamic sized list and column count and perform a type of pivoting for you, returning an array of arrays in the desired format which you should be able to iterate through and generate your columns, give it a try:

    rule!chris_pivot()

    with(
    local!array: {"a","b","c","d","e","f","g","h","i"},
    local!columns: 3,
    local!count: count(local!array),
    local!indexes: apply(rule!chris_pivotGetIndex(separation: local!columns, num: _, count: _),
    merge(
    1+enumerate(local!columns),
    apply(rule!chris_pivotGetSizes(max: local!count, size: local!columns, start: _), 1+enumerate(local!columns))
    )
    ),
    local!newArrays: apply(rule!chris_pivotGetItems(array: local!array, indexes: _),local!indexes),

    local!newArrays
    )

    Helper Rules:

    rule!chris_pivotGetIndex(num, separation, count) - inputs single valued integers:

    joinarray(
    apply(rule!chris_pivotGetIndex_helper(num: ri!num, separation: ri!separation, index: _), enumerate(ri!count))
    ,","
    )

    rule!chris_pivotGetIndex_helper(num, separation, count) - inputs single valued integers:

    (ri!index * ri!separation) + ri!num

    rule!chris_pivotGetSizes(max, start, size) - inputs single valued integers:

    ceiling((ri!max+1-ri!start)/ri!size)

    rule!chris_pivotGetItems(array,index) - both inputs text, 'array' is multiple:

    index(ri!array,apply(fn!tointeger,split(ri!indexes,",")),null)
  • @csteward: Thanks for sharing. I have tried to test the rules shared but they didnot render the expected {a,d,g},{b,e,h},{c,f,i} output. I am thinking of another approach too. But, if we can get your solution to work it will be more dynamic. Hope that it worked when you tried it.
  • Hi mohamedb, the rules output the desired format in my environment - what results are you seeing?
  • The parameters for each rule do not match the variables used in the rule. When I updated them to avoid an error, I got an empty array as a result.
  • @Appian: Drawing the Grid above is a straight forward nested loops in java. It is also a very common scenario. Is there an easy way to do the same in Appian?
  • Sorry for a few typos, few notes:

    rule!chris_pivotGetIndex_helper(num, separation, count)

    should be:

    rule!chris_pivotGetIndex_helper(num, separation, index)

    rule!chris_pivotGetItems(array,index)

    should be:

    rule!chris_pivotGetItems(array,indexes)

    Ensure in the last one above, array is multiple and indexes is not (single valued string). If the rest of the parameter types match from my initial post, you should be seeing data..