In my last post, I covered using guards with Erlang’s ETS select/2 functionality.
However, what if you’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’s some weird behavior though, with the last parameter of the Result part of the record. It seems that using ‘$$’ returns 0.
erlang
- 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’ll need to use the ets:select function, which has support for guards. Here’s a basic setup. Tmp = ets:new(tmp, []). ets:insert(Tmp, {bob, 0}). ets:insert(Tmp, {jim, 1}). ets:insert(Tmp, {jon, 2}). % Returns all results ets:select(Tmp,[{{'$1','$2'},[],['$$']}]).
- 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’}}).