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.

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

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
 

2 Responses to Erlang – Working with ets:select_count

  1. Ulf Wiger says:

    Weird, perhaps, but documented. ;-)
    Both ets:select_count/2 and ets:select_delete/2 expect the match to return true for matching objects.

  2. jon says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>