nth
nth <iterable> <n>
The element of an iterable at a 0-based index.
Arguments
<iterable>— a list or any iterable. A bare string is not iterable; convert it first withwords.<n>— an int index, counting from0. A non-positive index selects the first element.
Returns
The element at index <n>, or null if the iterable has fewer than
<n>+1 elements.
Examples
after command (say) {
do "say The second word is [nth [words "alpha beta gamma"] 1]."
}
Index 1 is the second element, so [nth ...] evaluates to "beta" and
the mob says "The second word is beta." Index 0 would give "alpha",
and a negative index also selects "alpha".
after command (say) {
let item [nth [words "alpha beta"] 9]
unless [isnull $item]
do "say I found $item."
}
The list has only two elements, so index 9 is out of range and
[nth ...] returns null; the unless guard then
skips the rest of the handler.
See also
first— shorthand for index 0.count— how many elements are available to index.slice— a bounded range of elements rather than one.- Iterables and iterables · Types