array_split

This page explains how to use the array_split function in APL.

The array_split function in APL splits an array into smaller subarrays based on specified split indices and packs the generated subarrays into a dynamic array. This function is useful when you want to partition data for analysis, batch processing, or distributing workloads across smaller units.

You can use array_split to:

  • Divide large datasets into manageable chunks for processing.
  • Create segments for detailed analysis or visualization.
  • Handle nested data structures for targeted processing.

Usage

Syntax

array_split(array, index)

Parameters

ParameterDescriptionType
arrayThe array to split.Dynamic
indexAn integer or dynamic array of integers. These zero-based split indices indicate the location at which to split the array.Integer or Dynamic

Returns

Returns a dynamic array containing N+1 arrays where N is the number of input indices. The original array is split at the input indices.

Use case examples

Single split index

Split large event arrays into manageable chunks for analysis.

['otel-demo-traces']
| where array_length(events) == 3
| extend split_events = array_split(events, 2)

Run in Playground

Output

[
  {
    "timestamp": 1734033733465219300,
    "name": "Enqueued"
  },
  {
    "name": "Sent",
    "timestamp": 1734033733465228500
  },
  {
    "timestamp": 1734033733465455900,
    "name": "ResponseReceived"
  }
]
[
  [
    {
      "timestamp": 1734033733465219300,
      "name": "Enqueued"
    },
    {
      "name": "Sent",
      "timestamp": 1734033733465228500
    }
  ],
  [
    {
      "timestamp": 1734033733465455900,
      "name": "ResponseReceived"
    }
  ]
]

This query splits the events array at index 2 into two subarrays for further processing.

Multiple split indeces

Divide traces into fixed-size segments for better debugging.

Query

['otel-demo-traces']
| where array_length(events) == 3
| extend split_events = array_split(events, dynamic([1,2]))

Run in Playground

Output

[
  {
    "attributes": null,
    "name": "Enqueued",
    "timestamp": 1734034755085206000
  },
  {
    "name": "Sent",
    "timestamp": 1734034755085215500,
    "attributes": null
  },
  {
    "attributes": null,
    "name": "ResponseReceived",
    "timestamp": 1734034755085424000
  }
]
[
  [
    {
      "timestamp": 1734034755085206000,
      "attributes": null,
      "name": "Enqueued"
    }
  ],
  [
    {
      "timestamp": 1734034755085215500,
      "attributes": null,
      "name": "Sent"
    }
  ],
  [
    {
      "attributes": null,
      "name": "ResponseReceived",
      "timestamp": 1734034755085424000
    }
  ]
]

This query splits the events array into three subarrays based on the indices [1,2].

  • array_index_of: Finds the index of an element in an array.
  • array_rotate_right: Rotates array elements to the right by a specified number of positions.
  • array_shift_left: Shifts array elements one position to the left, moving the first element to the last position.

Other query languages