nth

nth <iterable> <n>

The element of an iterable at a 0-based index.

Arguments

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