/
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 - General Operations</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_41.html">previous</A>, <A HREF="ProgrammersManual_43.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="SEC42" HREF="ProgrammersManual_toc.html#TOC42">General Operations Applicable to all Values</A></H4>

<P>
<DL>
<DT><U>Function:</U> int <B>typeof</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX2"></A>
Takes any MOO value and returns an integer representing the type of <VAR>value</VAR>.
The result is the same as the initial value of one of these built-in variables:
<CODE>INT</CODE>, <CODE>FLOAT</CODE>, <CODE>STR</CODE>, <CODE>LIST</CODE>, <CODE>OBJ</CODE>, or <CODE>ERR</CODE>.
Thus, one usually writes code like this:

</P>

<PRE>
if (typeof(x) == LIST) ...
</PRE>

<P>
and not like this:

</P>

<PRE>
if (typeof(x) == 3) ...
</PRE>

<P>
because the former is much more readable than the latter.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> str <B>tostr</B> <I>(<VAR>value</VAR>, ...)</I>
<DD><A NAME="IDX3"></A>
Converts all of the given MOO values into strings and returns the concatenation
of the results.

</P>

<PRE>
tostr(17)                  =>   "17"
tostr(1.0/3.0)             =>   "0.333333333333333"
tostr(#17)                 =>   "#17"
tostr("foo")               =>   "foo"
tostr({1, 2})              =>   "{list}"
tostr(E_PERM)              =>   "Permission denied"
tostr("3 + 4 = ", 3 + 4)   =>   "3 + 4 = 7"
</PRE>

<P>
Note that <CODE>tostr()</CODE> does not do a good job of converting lists into
strings; all lists, including the empty list, are converted into the string
<CODE>"{list}"</CODE>.  The function <CODE>toliteral()</CODE>, below, is better for this
purpose.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> str <B>toliteral</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX4"></A>
Returns a string containing a MOO literal expression that, when evaluated,
would be equal to <VAR>value</VAR>.

</P>

<PRE>
toliteral(17)         =>   "17"
toliteral(1.0/3.0)    =>   "0.333333333333333"
toliteral(#17)        =>   "#17"
toliteral("foo")      =>   "\"foo\""
toliteral({1, 2})     =>   "{1, 2}"
toliteral(E_PERM)     =>   "E_PERM"
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> int <B>toint</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX5"></A>
<DT><U>Function:</U> int <B>tonum</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX6"></A>
Converts the given MOO value into an integer and returns that integer.
Floating-point numbers are rounded toward zero, truncating their fractional
parts.  Object numbers are converted into the equivalent integers.  Strings are
parsed as the decimal encoding of a real number which is then converted to an
integer.  Errors are converted into integers obeying the same ordering (with
respect to <CODE>&#60;=</CODE> as the errors themselves.  <CODE>Toint()</CODE> raises
<CODE>E_TYPE</CODE> if <VAR>value</VAR> is a list.  If <VAR>value</VAR> is a string but the
string does not contain a syntactically-correct number, then <CODE>toint()</CODE>
returns 0.

</P>

<PRE>
toint(34.7)        =>   34
toint(-34.7)       =>   -34
toint(#34)         =>   34
toint("34")        =>   34
toint("34.7")      =>   34
toint(" - 34  ")   =>   -34
toint(E_TYPE)      =>   1
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> obj <B>toobj</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX7"></A>
Converts the given MOO value into an object number and returns that object
number.  The conversions are very similar to those for <CODE>toint()</CODE> except
that for strings, the number <EM>may</EM> be preceded by <SAMP>`#'</SAMP>.

</P>

<PRE>
toobj("34")       =>   #34
toobj("#34")      =>   #34
toobj("foo")      =>   #0
toobj({1, 2})     error-->   E_TYPE
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> float <B>tofloat</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX8"></A>
Converts the given MOO value into a floating-point number and returns that
number.  Integers and object numbers are converted into the corresponding
integral floating-point numbers.  Strings are parsed as the decimal encoding of
a real number which is then represented as closely as possible as a
floating-point number.  Errors are first converted to integers as in
<CODE>toint()</CODE> and then converted as integers are.  <CODE>Tofloat()</CODE> raises
<CODE>E_TYPE</CODE> if <VAR>value</VAR> is a list.  If <VAR>value</VAR> is a string but the
string does not contain a syntactically-correct number, then <CODE>tofloat()</CODE>
returns 0.

</P>

<PRE>
tofloat(34)          =>   34.0
tofloat(#34)         =>   34.0
tofloat("34")        =>   34.0
tofloat("34.7")      =>   34.7
tofloat(E_TYPE)      =>   1.0
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> int <B>equal</B> <I>(<VAR>value1</VAR>, <VAR>value2</VAR>)</I>
<DD><A NAME="IDX9"></A>
Returns true if <VAR>value1</VAR> is completely indistinguishable from <VAR>value2</VAR>.
This is much the same operation as "<CODE><VAR>value1</VAR> == <VAR>value2</VAR></CODE>"
except that, unlike <CODE>==</CODE>, the <CODE>equal()</CODE> function does not treat
upper- and lower-case characters in strings as equal.

</P>

<PRE>
"Foo" == "foo"         =>   1
equal("Foo", "foo")    =>   0
equal("Foo", "Foo")    =>   1
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> int <B>value_bytes</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX10"></A>
Returns the number of bytes of the server's memory required to store the given
<VAR>value</VAR>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> str <B>value_hash</B> <I>(<VAR>value</VAR>)</I>
<DD><A NAME="IDX11"></A>
Returns the same string as <CODE>string_hash(toliteral(<VAR>value</VAR>))</CODE>; see the
description of <CODE>string_hash()</CODE> for details.
</DL>

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