The following slice notation can be used:
list[start:stop:step]
The start
and stop
value will default to nil
and step
has a default value of 1
.
For understanding slices it is important to remember that the :stop
value represents the
first value that is not in the selected slice. This means that the
difference between stop
and start
is the number of selected items.
(assuming that step
is the default value 1
)
Another feature is that negative numbers may be used. For start
and stop
this means that it
will count from the end of the array instead of the beginning.
Some examples using
start
andstop
to select some items:
months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
[
{ months[:2]; /* first two months */ },
{ months[-2:]; /* last two months */ },
{ months[:-2]; /* all, except the last two months */ },
];
…and the corresponding result in JSON format:
[
[
"January",
"February"
],
[
"November",
"December"
],
[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October"
]
]
Another feature is the step
argument, which also may be negative to reverse the
slice direction.
Some examples using the
step
value:
months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
[
{ months[::2]; /* only the odd months */ },
{ months[::-1]; /* months in reverse order, equal to months.reverse() */ },
];
…the corresponding result in JSON format:
[
[
"January",
"March",
"May",
"July",
"September",
"November"
],
[
"December",
"November",
"October",
"September",
"August",
"July",
"June",
"May",
"April",
"March",
"February",
"January"
]
]
Slices can also be used to replace the splice function.
It is not possible to use the step
value when assigning items using the slice syntax.
Example using the slice syntax to replace items in a list:
months = ['January', 'February', 'XXX', 'May'];
/* Replace 'XXX' with 'March' and 'April' */
months[2:3] = ['March', 'April'];
/* Return result */
months;
Once again the result in JSON format:
[
"January",
"February",
"March",
"April",
"May"
]
Instead of lists and tuples it also possible to use the slice notion on strings
Slice on string example:
/* Return 'abcdef' in reverse order */
"abcdef"[::-1];
"fedcba"