-
Notifications
You must be signed in to change notification settings - Fork 24
Slicing
Rohan Singh edited this page Jun 11, 2017
·
1 revision
Slicing is an operation that can be performed on array
and string
values to pick out a subset of values.
var values = [ 1, 1, 2, 3, 5, 8, 13, 21 ];
var slice1 = values[1:6];
printLn(slice1.serialize()); // => [ 1, 2, 3, 5, 8, 13 ]
var slice2 = values[1:6:2];
printLn(slice2.serialize()); // => [ 1, 3, 8 ]
The slice takes three parameters: start index, end index, and step value. Negative indices will count backwards from the last value.
All of these parameters are optional:
- Start index will default to
0
. - End index will default to the last index.
- Step value will be
1
when the start index is less than the end index, otherwise it will be-1
.
Slicing can be enabled for object
values by implementing the __slice
metamethod.