[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] Extracting an Element from a List or String

The indexing expression in MOO extracts a specified element from a list or string:


First, expression-1 is evaluated; it must return a list or a string (the sequence). Then, expression-2 is evaluated and must return an integer (the index). If either of the expressions returns some other type of value, E_TYPE is returned. The index must be between 1 and the length of the sequence, inclusive; if it is not, then E_RANGE is raised. The value of the indexing expression is the index'th element in the sequence. Anywhere within expression-2, you can use the symbol $ as an expression returning the length of the value of expression-1.

"fob"[2]                =>  "o"
"fob"[1]                =>  "f"
{#12, #23, #34}[$ - 1]  =>  #23

Note that there are no legal indices for the empty string or list, since there are no integers between 1 and 0 (the length of the empty string or list).

Fine point: The $ expression actually returns the length of the value of the expression just before the nearest enclosing [...] indexing or subranging brackets. For example:
"frob"[{3, 2, 4}[$]]     =>  "b"

This document was generated by Roger Crew on June, 2 2004 using texi2html