Erlang - Working With Ets:select_count
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. Instead, it looks like you need to use true.
If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.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