words

words <string>

Split a string on whitespace into an iterable of its words.

This is the way to make a string iterable. A bare string is not an iterable, so before you can feed text to each, count, first, select, or any other consumer, you convert it with words.

Arguments

Returns

An iterable whose elements are string words, in order. Each element is one whitespace-delimited token of the input.

Examples

after command (say) {
  do "say There are [count [words "one two three"]] words."
}

[words "one two three"] yields three words, so [count ...] evaluates to 3 and the mob says "There are 3 words."

after command (say) {
  each [words "north east south west"] { <dir>
    do "say I could go $dir."
  }
}

The iterable drives each, running the block once per word — the mob names each direction in turn. The block parameter <dir> is declared inside the braces, right after {.

after command (say) {
  do "say first=[first [words "alpha beta"]]"
}

[first [words "alpha beta"]] evaluates to "alpha".

The automatic $args binding is already a word iterable, so you do not wrap it in words; reach for words when you have a plain string such as $arg or a literal.

See also