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

Parents
  • 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)
Reply
  • 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)
Children
No Data