Erlang - Working with ets:select_count
October 15, 2008 – 3:47 pmIn 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. Instead, it looks like you need to use true.
1> Tmp = ets:new(bob, []).
38
2> ets:insert(Tmp, {bob, 3}).
true
3> ets:select_count(Tmp, [{ {bob, '$1'}, [{ '>', '$1', 0}], [true]}]).
1
4> ets:select_count(Tmp, [{ {bob, '$1'}, [{ '>', '$1', 0}], ['$$']}]).
0



2 Responses to “Erlang - Working with ets:select_count”
Weird, perhaps, but documented.
Both ets:select_count/2 and ets:select_delete/2 expect the match to return true for matching objects.
By Ulf Wiger on Oct 16, 2008
Just because something’s documented somewhere doesn’t mean it’ s not worth bringing up. I’ll admit, I didn’t see the documentation, so there’s a chance someone else won’t either.
By jon on Oct 16, 2008