slice
slice <iterable> <start> [end]
A bounded sub-range of an iterable, as a new iterable.
slice takes two or three arguments. With a start only, it yields every
element from that index onward; with a start and an end, it yields the
elements at indices start up to but not including end.
Arguments
<iterable>— a list or any iterable. A bare string is not iterable; convert it first withwords.<start>— an int, the 0-based index of the first element to keep. A negativestartis treated as0.[end]— an optional int, the 0-based index to stop before. When omitted, the slice is unbounded and runs to the end of the iterable. Astart/endpair that selects nothing (for exampleendnot paststart) produces an empty slice.
Returns
An iterable over the selected elements. Consume it with
each, count, first, or another
iterable consumer.
Examples
after command (say) {
do "say middle = [count [slice [words "a b c d e"] 1 3]]"
}
slice ... 1 3 keeps the elements at indices 1 and 2 ("b" and "c"),
so [count ...] evaluates to 2 and the mob says "middle = 2."
after command (say) {
each [slice [words "a b c d e"] 2] { <w>
do "say tail word $w"
}
}
With no end, the slice runs from index 2 to the end — "c", "d", and
"e" — and the mob says one line per word. (Counting that same slice gives
3.)