Functions Index :: A :: ArraySplit

[<< up one level]


array :: slices


ArraySplit(string array, int index, int "chunks", int "chunksize")


Splits array to a specified number of parts or to parts with specified length (ie number of elements) and returns the requested part.


array: The array to split.

index: The zero-based index of the array part to return.

If chunks is specified, index must be in the range [0..chunks - 1].

If chunksize is specified, index must be in the range [0..Ceil(ArrayLen(array) / chunksize) - 1].

"chunks" (Optional): The number of parts to split array to.

"chunksize" (Optional): The number of elements that each array part should have.

Notes and conditions on arguments relations:

1] Exactly one of the chunks, chunksize arguments must be specified. If none or both are specified the function will throw an error.

2] If chunks is specified all parts except the last will have Floor(ArrayLen(array) / chunks) elements.

3] If chunksize is specified all parts except the last will have chunksize elements.

4] In any case the last part will contain the remaining array elements (which number may be equal to that of the other parts for certain combinations of array length and chunks or chunksize but in general will not).


a1 = "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16"

# Splitting a 17-element array in 5 chunks yields

# four 3-element arrays and a last 5-element array.


a2 = a1.ArraySplit(0, 5) # a2 == "0,1,2"

a3 = a1.ArraySplit(2, 5) # a3 == "6,7,8"

a4 = a1.ArraySplit(4, 5) # a4 == "12,13,14,15,16"


# Splitting a 17-element array with chunksize 4 yields

# four 4-element arrays and a last 1-element array.


a5 = a1.ArraySplit(0, chunksize=4) # a5 == "0,1,2,3"

a6 = a1.ArraySplit(2, chunksize=4) # a6 == "8,9,10,11"

a7 = a1.ArraySplit(4, chunksize=4) # a7 == "16"


[<< top]