Quirrel4.41.0

types.Array.slice

Method on every array
array.slice([start: number, end: number], ...): any

Copies the elements from start up to, but not including, end.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.
examples/types/array/slice-basic.nut
let squad = ["scout", "medic", "sniper", "engineer"];
let frontline = squad.slice(1, 3);
println("frontline =", ", ".join(frontline));
Output:
frontline = medic, sniper

Parameters

startnumberfirst index to copy; 0 when left out optional
endnumberindex to stop before; len() when left out optional
...any repeats

Return value

A new array holding the copied elements, empty if the range is empty.

Notes

Despite the trailing ... in the signature above, slice is not variadic: it takes zero, one or two real arguments, and a third is silently ignored rather than erroring, unlike findvalue. A negative start or end counts back from the end (-1 is the last element), and the range is clamped into [0, len()] rather than throwing when it runs past either end.

Example

examples/types/array/slice.nut
let a = [0, 1, 2, 3, 4];
println("a.slice().len() =", a.slice().len());        // no arguments: a full copy
println("a.slice(2).len() =", a.slice(2).len());        // one argument: from start to the end
println("a.slice(-2, -1) =", ", ".join(a.slice(-2, -1).map(@(v) v.tostring())));   // negative indices count from the end
println("a.slice(1, 3, 999).len() =", a.slice(1, 3, 999).len());   // a third argument is silently ignored
Output:
a.slice().len() = 5
a.slice(2).len() = 3
a.slice(-2, -1) = 3
a.slice(1, 3, 999).len() = 2

See also

resizeGrows or shrinks the array to size elements.
sliceReturns the bytes of str from start up to, but not including, end.
arrayclass index