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’s a very, very basic intro do doing matching with ETS and erlang. It’s similar to a SELECT in SQL.

Here’s some simple matching.

44> Tmp = ets:new(bacon, []). 26 45> ets:insert(Tmp, {joe, {fish, 1}}). true 46> ets:match(Tmp, {‘$1’, {fish, ‘$2’}}). [[joe,1]]

What’s going on here? Each of your placeholders end up showing up in the result, in a tuple.

You didn’t need to know it was joe who had fish?

47> ets:match(Tmp, {’_‘, {fish, ‘$2’}}). [[1]]

Lets add a few more guys. Same deal.

48> ets:insert(Tmp, {bob, {fish, 1}}). true 52> ets:insert(Tmp, {steve, {bacon, 1}}). true 53> ets:insert(Tmp, {rex, {bacon, 1}}). true 54> ets:insert(Tmp, {alfonzo, {bacon, 1}}). true

Who here’s got fish?

55> ets:match(Tmp, {‘$1’, {fish, ‘$2’}}). [[joe,1],[bob,1]]

Basically, you put the same pattern in the match, and the places that are wild cards that you want to save, you use a ‘$1’ type constant. The ones you don’t care about, use an underscore.

Of course, reading the original ETS specifications might be useful. They review the actual matching spec on the select/2 page.

For a more advanced tutorial covering matching using guards, check out this follow up post on select/2.

.