ARRAYSLICE Function

Returns an array containing a slice of the input array, as determined by starting and ending index parameters.
  • Starting index parameter is required. A value of 0 indicates the first element of the array.
  • Ending index parameter is optional.
    • Ending index value is 0-based and not inclusive.
    • Default value is empty, which indicates the end of the array.

Basic Usage

Array literal reference example:

derive type:single value:ARRAYSLICE(["A","B","C","D"],1,2)

Output: Generates an output column containing the array: ["B"].

Column reference example:

derive type:single value:ARRAYSLICE([myValues],2) as:mySliceOfmyValues'

Output: Generates the new mySliceOfmyValues column containing a slice of the arrays in the myValues, starting at the third value and extending to the end of the array.

Syntax

derive type:single value:ARRAYSLICE(array_ref,int_start_index,[int_end_index])

ArgumentRequired?Data TypeDescription
array_refYarray or stringName of Array column, Array literal, or function returning an Array to apply to the function
int_start_indexYinteger0-based index value of the first element in the Array to include in the slice.
int_end_indexNinteger

0-based index "soft" value of the last element in the Array to include in the slice. Listed value is not included.

If no value is provided, the last element of the array is the end of the slice.

For more information on syntax standards, see Language Documentation Syntax Notes.

array_ref

Name of the array column, array literal, or function returning an array whose element you want to locate.

  • Multiple columns and wildcards are not supported.

Usage Notes:

Required?Data TypeExample Value
YesString (column reference or function) or array literalmyArray1

int_start_index

Index of the starting element of the source array that you wish to include in the slice.

  • A value of 0 captures the first element of the array.
  • If this value is greater than the total number of elements in the source array, an empty array is returned as the slice.

Usage Notes:

Required?Data TypeExample Value
YesInteger (whole number)4

int_end_index

Optional index of the ending element of the source array that you wish to include in the slice.

  • A value of 0 captures the first element of the array.
  • The value indicated by this parameter is not included in the slice.
  • If the end index value is specified, it must be greater than or equal to the start index value.
  • If this value is greater than the total number of elements in the source array, then the slice ends at the final element of the array.

Usage Notes:

Required?Data TypeExample Value
YesInteger (whole number)10

Examples

Example - Podium Race Finishes

This example covers the following functions:

  • ARRAYSLICE - Returns an array that is a slice of another array, based on the provided starting and ending index numbers. See ARRAYSLICE Function.
  • ARRAYMERGEELEMENTS - Merges the elements of an array together into a string. See ARRAYMERGEELEMENTS Function.

Source:

The following set of arrays contain results, in order, of a series of races. From this list, the goal is to extract a list of the podium finishers for each race as a single string.

RaceIdRaceResults
1["racer3","racer5","racer2","racer1","racer6"]
2["racer6","racer4","racer2","racer1","racer3","racer5"]
3["racer4","racer3","racer5","racer2","racer6","racer1"]
4["racer1","racer2","racer3","racer5"]
5["racer5","racer2","racer4","racer6","racer3"]


Transform:

From the list of arrays, the first step is to gather the top-3 finishers from each race:

derive type: single value: ARRAYSLICE(RaceResults, 0, 3) as: 'arrPodium'

The above captures the first three values of the RaceResults arrays into a new set of arrays.

The next step is to merge this new set of arrays into a single string:

derive type: single value: ARRAYMERGEELEMENTS(arrPodium, ',') as: 'strPodium'

Results:

RaceIdRaceResultsarrPodiumstrPodium
1["racer3","racer5","racer2","racer1","racer6"]["racer3","racer5","racer2"]racer3,racer5,racer2
2["racer6","racer4","racer2","racer1","racer3","racer5"]["racer6","racer4","racer2"]racer6,racer4,racer2
3["racer4","racer3","racer5","racer2","racer6","racer1"]["racer4","racer3","racer5"]racer4,racer3,racer5
4["racer1","racer2","racer3","racer5"]["racer1","racer2","racer3"]racer1,racer2,racer3
5["racer5","racer2","racer4","racer6","racer3"]["racer5","racer2","racer4"]racer5,racer2,racer4


Var denne siden nyttig? Si fra hva du synes:

Send tilbakemelding om ...

Google Cloud Dataprep Documentation
Trenger du hjelp? Gå til brukerstøttesiden vår.