<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rusty Razor Blade &#187; erlang</title>
	<atom:link href="http://www.rustyrazorblade.com/category/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Wed, 21 Jul 2010 20:42:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Debugging with Erlang</title>
		<link>http://www.rustyrazorblade.com/2010/07/debugging-with-erlang/</link>
		<comments>http://www.rustyrazorblade.com/2010/07/debugging-with-erlang/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 23:58:12 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1110</guid>
		<description><![CDATA[First, make sure you have the compile flag (+debug_info) set when compiling your source, then fire up the debugger:
1> i:im(). 
My Erlang Makefile:

EBIN_DIR := ebin
SRC_DIR := src
EXAMPLES_DIR := examples
INCLUDE_DIR := include
ERLC := erlc
ERLC_FLAGS := +debug_info +native -W -I $(INCLUDE_DIR) -o $(EBIN_DIR)

all:
	@mkdir -p $(EBIN_DIR)
	$(ERLC) $(ERLC_FLAGS) $(SRC_DIR)/*.erl

clean:
	@rm -rf $(EBIN_DIR)/*
	@rm -f erl_crash.dump

Read up on the Erlang docs.
The debugger [...]]]></description>
			<content:encoded><![CDATA[<p>First, make sure you have the compile flag (+debug_info) set when compiling your source, then fire up the debugger:</p>
<pre>1> i:im(). </pre>
<p>My Erlang Makefile:</p>
<pre>
EBIN_DIR := ebin
SRC_DIR := src
EXAMPLES_DIR := examples
INCLUDE_DIR := include
ERLC := erlc
ERLC_FLAGS := +debug_info +native -W -I $(INCLUDE_DIR) -o $(EBIN_DIR)

all:
	@mkdir -p $(EBIN_DIR)
	$(ERLC) $(ERLC_FLAGS) $(SRC_DIR)/*.erl

clean:
	@rm -rf $(EBIN_DIR)/*
	@rm -f erl_crash.dump
</pre>
<p><a href="http://www.erlang.org/doc/apps/debugger/debugger_chapter.html">Read up on the Erlang docs.</a></p>
<p>The debugger can be a bit weird in that it doesn&#8217;t always find your ebin directory (if you&#8217;re compiling to a separate ebin dir)&#8230; it took me a bit to figure out.  Also on the Mac it seems to either crash or not launch timetimes.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2010/07/debugging-with-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Erlang Code from the Command Line</title>
		<link>http://www.rustyrazorblade.com/2010/07/running-erlang-code-from-the-command-line/</link>
		<comments>http://www.rustyrazorblade.com/2010/07/running-erlang-code-from-the-command-line/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 18:31:31 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1105</guid>
		<description><![CDATA[This was really useful for me in scripting TextEdit to run my unit tests, as Erlide has been crashing every time I use it.

erl -run mymodule myfunc -run init stop -noshell

http://www.trapexit.org/Running_Erlang_Code_From_The_Command_Line
]]></description>
			<content:encoded><![CDATA[<p>This was really useful for me in scripting TextEdit to run my unit tests, as Erlide has been crashing every time I use it.</p>
<pre>
erl -run mymodule myfunc -run init stop -noshell
</pre>
<p><a href='Running Erlang Code From the Command Line'>http://www.trapexit.org/Running_Erlang_Code_From_The_Command_Line</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2010/07/running-erlang-code-from-the-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang: Understanding gen_server</title>
		<link>http://www.rustyrazorblade.com/2009/04/erlang-understanding-gen_server/</link>
		<comments>http://www.rustyrazorblade.com/2009/04/erlang-understanding-gen_server/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 18:08:47 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[gen_server]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=538</guid>
		<description><![CDATA[Gen_server is a great way to create simple servers without having to write a lot of code at all.  Here&#8217;s a brief overview to get you started.
For some reason, figuring out how the gen_server behavior works in erlang was kind of a pain for me.  I think it&#8217;s because I can&#8217;t just implement [...]]]></description>
			<content:encoded><![CDATA[<p>Gen_server is a great way to create simple servers without having to write a lot of code at all.  Here&#8217;s a brief overview to get you started.</p>
<p>For some reason, figuring out how the <a href="http://erlang.org/doc/man/gen_server.html">gen_server </a>behavior works in erlang was kind of a pain for me.  I think it&#8217;s because I can&#8217;t just implement something, I need to know <em>why</em> it works.  Well, now I know.</p>
<p>So, lets get started.  <a href="http://erlang.org/doc/man/gen_server.html">Gen_server </a>is built into erlang.  It abstracts the message passing away.  As far as I can tell, yes, you could write this functionality yourself.  But why would you want to when it&#8217;s done for you?</p>
<p>For starters, there&#8217;s a few functions you need to implement.  If you&#8217;re using <a href="http://erlide.sourceforge.net/">Erlide</a>, you can create a new module and choose <a href="http://erlang.org/doc/man/gen_server.html">gen_server </a>as the behavior, and it&#8217;ll give you a skeleton.  Nifty.  I strongly recommend going this route, as it has a lot of helpful comments in the code.  </p>
<p>Lets write an addition and subtraction server.  It&#8217;s really not that useful, but it&#8217;s enough to get started and understand what&#8217;s happening.</p>
<p><strong>Init</strong></p>
<blockquote><p>
%% &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
%% Function: init/1<br />
%% Description: Initiates the server<br />
%% Returns: {ok, State}          |<br />
%%          {ok, State, Timeout} |<br />
%%          ignore               |<br />
%%          {stop, Reason}<br />
%% &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
init([]) -><br />
    {ok, 0}.
</p></blockquote>
<p>Here&#8217;s a very important thing to understand.   <strong>A <a href="http://erlang.org/doc/man/gen_server.html">gen_server </a>maintains a state.</strong>  The State that&#8217;s returned above is the starting state of the server.  In our case, the initial state will be zero.</p>
<p><strong>handle_call</strong><br />
Now we want to add some functionality.  Lets add addition and subtraction.  We&#8217;ll need to export our new functions as well, so lets make sure we put this in with the exports:</p>
<blockquote><p>-export([add/1, start/0, subtract/1]).</p></blockquote>
<p>And the actual code:</p>
<blockquote><p>add(Num) -> gen_server:call( ?MODULE, {add, Num}).<br />
subtract(Num) -> gen_server:call( ?MODULE, {subtract, Num}).</p>
<p>handle_call({add, Num}, _From, State) -> {reply, State + Num, State + Num};<br />
handle_call({subtract, Num}, _From, State) -> {reply, State &#8211; Num, State &#8211; Num};
</p></blockquote>
<p>When you call counting:add, it will in turn call the gen_server:call function, which takes a module and a parameter.  It will then call &#8220;handle_call&#8221; with this paramater, the Pid it came from, and the State of the server.  The parameter can be anything, so if we use a tuple, we can take advantage of erlang&#8217;s pattern matching.  handle_call will return what the gen_server should do next &#8211; reply (return a result), noreply, or stop.  In this case, we&#8217;re replying.  </p>
<p>The second parameter is the reply.  We&#8217;re just going to return the current count here.  </p>
<p>We also need to return the new server state in parameter 3.  I chose to put the new state in param 2 because it&#8217;s useful, but we could just as easily put anything there &#8211; an atom, a random string, whatever.  </p>
<p>There&#8217;s a lot of options within there, so you might want to check the docs.</p>
<p>I STRONGLY recommend using <a href="http://erlide.sourceforge.net/">Erlide </a>for your development.  It auto compiles and gives you an interactive shell right in the editor.  It made Erlang development so much easier.  It&#8217;ll also create the stubs for the other functions you&#8217;ll need (terminate, code change, handle_info, handle_cast).</p>
<p>FYI, If you get an error like this:</p>
<blockquote><p>** exception exit: {noproc,{gen_server,call,[calculator,{add,1}]}}<br />
     in function  gen_server:call/2</p></blockquote>
<p>It&#8217;s because your gen_server isn&#8217;t actually running.  Make sure you start it before you test with </p>
<blockquote><p>calculator:start().</p></blockquote>
<p>Hopefully this is enough to get you started.  Read up more in the <a href="http://erlang.org/doc/man/gen_server.html">official Erlang docs for gen_server</a>.  Scroll to the bottom to find the specifications on what functions you must implement, and what they need to return.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/04/erlang-understanding-gen_server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang: Create schema before starting mnesia</title>
		<link>http://www.rustyrazorblade.com/2009/04/erlang-create-schema-before-starting-mnesia/</link>
		<comments>http://www.rustyrazorblade.com/2009/04/erlang-create-schema-before-starting-mnesia/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 23:57:19 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=591</guid>
		<description><![CDATA[If you&#8217;re running into trouble with Mnesia not writing your data to disk, make sure you create the schema BEFORE you start mnesia.  Otherwise you&#8217;ll get errors like
opt_disc. Directory &#8220;c:/Documents and Settings/jhaddad/workspace/Mnesia.localhost@whatever&#8221; is NOT used.
]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re running into trouble with Mnesia not writing your data to disk, make sure you create the schema BEFORE you start mnesia.  Otherwise you&#8217;ll get errors like</p>
<blockquote><p>opt_disc. Directory &#8220;c:/Documents and Settings/jhaddad/workspace/Mnesia.localhost@whatever&#8221; is NOT used.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/04/erlang-create-schema-before-starting-mnesia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang Records Cheatsheet</title>
		<link>http://www.rustyrazorblade.com/2008/11/erlang-records-cheatsheet/</link>
		<comments>http://www.rustyrazorblade.com/2008/11/erlang-records-cheatsheet/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 02:59:06 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=382</guid>
		<description><![CDATA[I hate looking stuff up.  I just like having this type of thing 1 click away, in a nice summary.  This post is mostly for me.
Define a record:
-record( rule, {ruleid, site, rule, original} ).
You can define a record in the shell using rd:
99> rd(rule, {ruleid, site, rule, original} ).
rule
Create an instance of a [...]]]></description>
			<content:encoded><![CDATA[<p>I hate looking stuff up.  I just like having this type of thing 1 click away, in a nice summary.  This post is mostly for me.</p>
<p><strong>Define a record:</strong></p>
<blockquote><p>-record( rule, {ruleid, site, rule, original} ).</p></blockquote>
<p>You can define a record in the shell using rd:</p>
<blockquote><p>99> rd(rule, {ruleid, site, rule, original} ).<br />
rule</p></blockquote>
<p><strong>Create an instance of a record:</strong></p>
<blockquote><p>100> A = #rule{ ruleid=1, site=2, rule=2, original=3}.<br />
#rule{ruleid = 1,site = 2,rule = 2,original = 3}</p></blockquote>
<p><strong>Access a single field in a record:</strong></p>
<blockquote><p>101> A#rule.original.<br />
3</p></blockquote>
<p><strong>Extracting multiple fields:</strong></p>
<blockquote><p>102> #rule{ruleid=B, site=C, rule=D, original=E} = A.<br />
#rule{ruleid = 1,site = 2,rule = 2,original = 3}<br />
103> B.<br />
1<br />
104> C.<br />
2<br />
105> D.<br />
2<br />
106> E.<br />
3</p></blockquote>
<p><strong>Recommended Reading</strong><br />
<a href="http://20bits.com/articles/erlang-an-introduction-to-records/">Introduction to Records</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2008/11/erlang-records-cheatsheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang: Installing Leex</title>
		<link>http://www.rustyrazorblade.com/2008/11/erlang-installing-leex/</link>
		<comments>http://www.rustyrazorblade.com/2008/11/erlang-installing-leex/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 01:59:23 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[leex]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=348</guid>
		<description><![CDATA[Leex is an erlang version of Lex, a Lexical Analyzer Generator written by Robert Virding.  Robert (and several others in #erlang on freenode) were incredibly helpful and considerate in helping me understand these tools.
Leex is a tokenizer.  It breaks the pieces of your file or text into tokens. You can then use a [...]]]></description>
			<content:encoded><![CDATA[<p>Leex is an erlang version of <a href="http://dinosaur.compilertools.net/lex/">Lex</a>, a Lexical Analyzer Generator written by Robert Virding.  Robert (and several others in #erlang on freenode) were incredibly helpful and considerate in helping me understand these tools.</p>
<p>Leex is a tokenizer.  It breaks the pieces of your file or text into tokens. You can then use a tool like yecc to take these tokens and generate useful parsers.</p>
<p>First, you&#8217;ll need the source.  <a href="http://www.trapexit.org/forum/viewtopic.php?p=43929">Leex source is available here</a>.  Go down to the last post in the forum, download the attached file.</p>
<ol>
<li>Create a directory for your erlang packages.  I chose ~/erlang.</li>
<li>Drop the leex directory in your new erlang package directory.</li>
<li>Edit your ~/.erlang file (it probably doesn&#8217;t exist yet) to include this line, making sure to use the path to your erlang package directory instead of mine:<br />
<blockquote><p>code:add_pathz(&#8221;/Users/jhaddad/erlang/leex/ebin&#8221;).</p></blockquote>
</li>
</ol>
<p>You should be able to use the leex libraries now.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2008/11/erlang-installing-leex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang &#8211; Working with ets:select_count</title>
		<link>http://www.rustyrazorblade.com/2008/10/erlang-working-with-etsselect_count/</link>
		<comments>http://www.rustyrazorblade.com/2008/10/erlang-working-with-etsselect_count/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 22:47:35 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=289</guid>
		<description><![CDATA[In my last post, I covered using guards with Erlang&#8217;s ETS select/2 functionality.  
However, what if you&#8217;re looking to select a count of the number of matches for a given pattern?  Lets use select_count for that.
You can use the same pattern matching I covered in my previous post.
There&#8217;s some weird behavior though, with [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post, I covered <a href="/2008/10/10/ets-matching-with-guards-select2/">using guards with Erlang&#8217;s ETS select/2 functionality</a>.  </p>
<p>However, what if you&#8217;re looking to select a count of the number of matches for a given pattern?  Lets use select_count for that.</p>
<p>You can use the same pattern matching I covered in my <a href="/2008/10/10/ets-matching-with-guards-select2/">previous post</a>.</p>
<p>There&#8217;s some weird behavior though, with the last parameter of the Result part of the record.  It seems that using &#8216;$$&#8217; returns 0.  Instead, it looks like you need to use true.</p>
<blockquote><p>1> Tmp = ets:new(bob, []).<br />
38<br />
2> ets:insert(Tmp, {bob, 3}).<br />
true<br />
3> ets:select_count(Tmp, [{ {bob, '$1'}, [{ '>', '$1', 0}], [true]}]).<br />
1<br />
4> ets:select_count(Tmp, [{ {bob, '$1'}, [{ '>', '$1', 0}], ['$$']}]).<br />
0</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2008/10/erlang-working-with-etsselect_count/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Erlang: ETS Matching with Guards (select/2)</title>
		<link>http://www.rustyrazorblade.com/2008/10/ets-matching-with-guards-select2/</link>
		<comments>http://www.rustyrazorblade.com/2008/10/ets-matching-with-guards-select2/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 00:35:07 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=269</guid>
		<description><![CDATA[Not every time we want to pull data out of an erlang ETS table is it as straightfoward as the previous example.  Sometimes we want to get all values that are greater than zero, rather than just constants. We&#8217;ll need to use the ets:select function, which has support for guards.
Here&#8217;s a basic setup.

Tmp = [...]]]></description>
			<content:encoded><![CDATA[<p>Not every time we want to pull data out of an erlang ETS table is it as straightfoward as the <a href="/2008/10/09/super-basic-ets-matching-tutorial/">previous example</a>.  Sometimes we want to get all values that are greater than zero, rather than just constants. We&#8217;ll need to use the ets:select function, which has support for guards.</p>
<p>Here&#8217;s a basic setup.</p>
<blockquote><p>
Tmp = ets:new(tmp, []).<br />
ets:insert(Tmp, {bob, 0}).<br />
ets:insert(Tmp, {jim, 1}).<br />
ets:insert(Tmp, {jon, 2}).</p>
<p>% Returns all results<br />
ets:select(Tmp,[{{'$1','$2'},[],['$$']}]).</p>
<p>% Returns results where $2 > 0 (jim and jon)</p>
<p>ets:select(Tmp,[{	{'$1','$2'},	[ { '>', '$2', 0 } ],['$$']	}]).</p>
</blockquote>
<p>What&#8217;s going on in the select?  There&#8217;s a LOT of syntax for something that&#8217;s so simple, but once we break it up a bit it starts to be easier to look at.</p>
<p>First off, lets take a look at the original spec.</p>
<blockquote><p>MatchSpec = [MatchFunction]<br />
MatchFunction = {MatchHead, [Guard], [Result]}<br />
MatchHead = &#8220;Pattern as in ets:match&#8221;<br />
Guard = {&#8221;Guardtest name&#8221;, &#8230;}<br />
Result = &#8220;Term construct&#8221;</p></blockquote>
<p>Pretty important stuff in here.  The basic building block of the MatchSpec is a list.  Inside the list, is a &#8220;matchfunction&#8221;.</p>
<p>[MatchFunction]</p>
<p>Next, we break the MatchFunction up a little bit more.  MatchHead = Pattern.  We&#8217;ve seen this before.</p>
<blockquote><p>MatchSpec = [ {Pattern, [Guard], [Result] } ]</p></blockquote>
<p>Now, lets write our basic pattern.  We want the rows where the 2nd item is > 0.  So, since we can&#8217;t put that in the pattern yet, we&#8217;ll just give it a reference.</p>
<blockquote><p>MatchSpec = [ { {$1, $2}, [Guard], [Result] } ]</p></blockquote>
<p>OK, cool.  That&#8217;s not so bad anymore!  Next step &#8211; lets use our guard.  There&#8217;s a funky syntax for guards in the matchspec, so here we go.  You&#8217;re looking at a list, right?  So it can have many items.  Each item is a record.  They can nest.  We&#8217;ll ignore that for now.  What we need to know here is that in a pattern matching guard, we put the operation first.  It would normally read $2 > 0.</p>
<blockquote><p>[ { '>', '$2', 0} ]</p></blockquote>
<p>That&#8217;ll give us the pattern we want.  What about the Result?  Feel free to just use &#8216;$$&#8217; in there.  It&#8217;ll just kick back what you specified in your pattern.  If you don&#8217;t believe me, </p>
<blockquote><p>8> ets:select(Tmp,[{ {'_','$2'},[ { '>', '$2', 0 } ],['$$']}]).<br />
[[1],[2]]</p></blockquote>
<p>Put it all together! </p>
<blockquote><p>ets:select(Tmp,[{ {'$1','$2'},        [ { '>', '$2', 0 } ],        ['$$']  }]).</p></blockquote>
<p>Not so bad anymore.  </p>
<p><a href="http://www.erlang.org/doc/man/ets.html#select-2">Match Spec docs for select/2</a></p>
<p>For testing purposes, you can use this bad boy.</p>
<blockquote><p>test_ms(Tuple, MatchSpec)</p></blockquote>
<p>You can also use this handy function to transform functions to selects:</p>
<p>fun2ms(LiteralFun) -> MatchSpec</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2008/10/ets-matching-with-guards-select2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Erlang: Super Basic ETS Matching Tutorial</title>
		<link>http://www.rustyrazorblade.com/2008/10/super-basic-ets-matching-tutorial/</link>
		<comments>http://www.rustyrazorblade.com/2008/10/super-basic-ets-matching-tutorial/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 00:39:33 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=251</guid>
		<description><![CDATA[I hate having to look stuff up to get examples, especially when I have to click on more than the first google link to figure things out.  As a result, here&#8217;s a very, very basic intro do doing matching with ETS and erlang.  It&#8217;s similar to a SELECT in SQL.
Here&#8217;s some simple matching.
44> [...]]]></description>
			<content:encoded><![CDATA[<p>I hate having to look stuff up to get examples, especially when I have to click on more than the first google link to figure things out.  As a result, here&#8217;s a very, very basic intro do doing matching with ETS and erlang.  It&#8217;s similar to a SELECT in SQL.</p>
<p>Here&#8217;s some simple matching.</p>
<blockquote><p>44> Tmp = ets:new(bacon, []).<br />
26<br />
45> ets:insert(Tmp, {joe, {fish, 1}}).<br />
true<br />
46> ets:match(Tmp, {&#8217;$1&#8242;, {fish, &#8216;$2&#8242;}}).<br />
[[joe,1]]
</p></blockquote>
<p>What&#8217;s going on here?  Each of your placeholders end up showing up in the result, in a tuple.</p>
<p>You didn&#8217;t need to know it was joe who had fish?</p>
<blockquote><p>47> ets:match(Tmp, {&#8217;_', {fish, &#8216;$2&#8242;}}).<br />
[[1]]</p></blockquote>
<p>Lets add a few more guys.  Same deal.</p>
<blockquote><p>
48> ets:insert(Tmp, {bob, {fish, 1}}).<br />
true<br />
52> ets:insert(Tmp, {steve, {bacon, 1}}).<br />
true<br />
53> ets:insert(Tmp, {rex, {bacon, 1}}).<br />
true<br />
54> ets:insert(Tmp, {alfonzo, {bacon, 1}}).<br />
true
</p></blockquote>
<p>Who here&#8217;s got fish?</p>
<blockquote><p>55> ets:match(Tmp, {&#8217;$1&#8242;, {fish, &#8216;$2&#8242;}}).<br />
[[joe,1],[bob,1]]</p></blockquote>
<p>Basically, you put the same pattern in the match, and the places that are wild cards that you want to save, you use a &#8216;$1&#8242; type constant.  The ones you don&#8217;t care about, use an underscore.</p>
<p>Of course, reading the <a href="http://www.erlang.org/doc/man/ets.html">original ETS specifications</a> might be useful.  They review the actual matching spec on the <a href="http://www.erlang.org/doc/man/ets.html#select-2">select/2 page</a>.</p>
<p>For a more advanced tutorial covering matching using guards, check out this <a href="/2008/10/10/ets-matching-with-guards-select2/">follow up post on select/2</a>.</p>
<p>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2008/10/super-basic-ets-matching-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
