1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>erl_format</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
<STYLE TYPE="text/css">
<!--
.REFBODY { margin-left: 13mm }
.REFTYPES { margin-left: 8mm }
-->
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
<IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>erl_format</H1>
</CENTER>
<H3>C LIBRARY</H3>
<DIV CLASS=REFBODY>
erl_format
</DIV>
<H3>C LIBRARY SUMMARY</H3>
<DIV CLASS=REFBODY>
Create and Match Erlang Terms
</DIV>
<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>
<P>This module contains two routines - one general function for
creating Erlang terms and one for pattern matching Erlang terms.
</DIV>
<H3>EXPORTS</H3>
<P><A NAME="erl_format/2"><STRONG><CODE>ETERM *erl_format(FormatStr, ... )</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>char *FormatStr;</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<P>This is a general function for creating Erlang terms using
a format specifier and a corresponding set of arguments, much
in the way <CODE>printf()</CODE> works.
<P><CODE>FormatStr</CODE> is a format specification string. The set
of valid format specifiers is as follows:
<P>
<UL>
<LI>
~i - Integer<BR>
</LI>
<LI>
~f - Floating point<BR>
</LI>
<LI>
~a - Atom<BR>
</LI>
<LI>
~s - String<BR>
</LI>
<LI>
~w - Arbitrary Erlang term<BR>
</LI>
</UL>
<P>For each format specifier that appears in <CODE>FormatStr</CODE>,
there must be a corresponding argument following
<CODE>FormatStr</CODE>. An Erlang term is built according to the
<CODE>FormatStr</CODE> with values and Erlang terms substituted from
the corresponding arguments and according to the individual
format specifiers. For example:
<PRE>
erl_format("[{name,~a},{age,~i},{data,~w}]",
"madonna",
21,
erl_format("[{adr,~s,~i}]","E-street",42));
</PRE>
<P>This will create an <CODE>(ETERM *)</CODE> structure corresponding
to the Erlang term:
<CODE>[{name,madonna},{age,21},{data,[{adr,"E-street",42}]}]</CODE>
<P>The function returns an Erlang term, or NULL if
<CODE>FormatStr</CODE> does not describe a valid Erlang term.
</DIV>
<P><A NAME="erl_match/2"><STRONG><CODE>int erl_match(Pattern, Term)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>ETERM *Pattern,*Term;</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<P>This function is used to perform pattern matching similar
to that done in Erlang. Refer to an Erlang manual for matching
rules and more examples.
<P><CODE>Pattern</CODE> is an Erlang term, possibly containing unbound
variables.
<P><CODE>Term</CODE> is an Erlang term that we wish to match against
<CODE>Pattern</CODE>.
<P><CODE>Term</CODE> and <CODE>Pattern</CODE> are compared, and any
unbound variables in <CODE>Pattern</CODE> are bound to corresponding
values in <CODE>Term</CODE>.
<P>If <CODE>Term</CODE> and <CODE>Pattern</CODE> can be matched, the
function returns a non-zero value and binds any unbound
variables in <CODE>Pattern</CODE>. If <CODE>Term</CODE> <CODE>Pattern</CODE> do
not match, the function returns 0. For example:
<PRE>
ETERM *term, *pattern, *pattern2;
term1 = erl_format("{14,21}");
term2 = erl_format("{19,19}");
pattern1 = erl_format("{A,B}");
pattern2 = erl_format("{F,F}");
if (erl_match(pattern1, term1)) {
/* match succeeds:
* A gets bound to 14,
* B gets bound to 21
*/
...
}
if (erl_match(pattern2, term1)) {
/* match fails because F cannot be
* bound to two separate values, 14 and 21
*/
...
}
if (erl_match(pattern2, term2)) {
/* match succeeds and F gets bound to 19 */
...
}
</PRE>
<P><CODE>erl_var_content()</CODE> can be used to retrieve the
content of any variables bound as a result of a call to
<CODE>erl_match()</CODE>.
</DIV>
<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
T.Trnkvist - support@erlang.ericsson.se<BR>
</DIV>
<CENTER>
<HR>
<SMALL>erl_interface 3.5.5.2<BR>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|