/
html/
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from ProgrammersManual.texinfo on 4 March 1997 -->

<TITLE>LambdaMOO Programmer's Manual - Extracting</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_17.html">previous</A>, <A HREF="ProgrammersManual_19.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>.
<P><HR><P>


<H4><A NAME="SEC18" HREF="ProgrammersManual_toc.html#TOC18">Extracting an Element from a List or String</A></H4>

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

</P>

<PRE>
<VAR>expression-1</VAR>[<VAR>expression-2</VAR>]
</PRE>

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

</P>

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

<P>
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).

</P>

<BLOCKQUOTE>
<P>
<EM>Fine point:</EM> The <CODE>$</CODE> expression actually returns the length of the
value of the expression just before the nearest enclosing <CODE>[...]</CODE>
indexing or subranging brackets.  For example:

<PRE>
"frob"[{3, 2, 4}[$]]     =>  "b"
</PRE>

</BLOCKQUOTE>

<P><HR><P>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_17.html">previous</A>, <A HREF="ProgrammersManual_19.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>.
</BODY>
</HTML>