[erlang-questions] Deleting from a duplicate_bag ets table

Bernard Duggan bernie@REDACTED
Wed Jan 21 23:36:46 CET 2009


Hi list,
    This is probably a really stupid question, but I'm damned if I can
figure it out:  If I have an ets table of type duplicate_bag, and I put
two of the same item in it, how do I delete just one of them?

In code:

T = ets:new(t, [duplicate_bag]),
ets:insert(T, {1, 2}),
ets:insert(T, {1, 2}),

At this point T contains two elements, both {1, 2}.  I now want to
delete one of them.  The options for deleting from an ets table seem to be:

ets:delete/2 - Deletes /all/ objects with the supplied key.
ets:delete_object/2 - Deletes /all/ copies of the supplied object.
ets:match_delete/2 - Deletes /all/ objects that match the pattern
ets:select_delete/2 - I'm not aware of any way to limit the number of
returns in a MatchSpec (and the presence of ets:select/3 where you can
supply a limit seems to support this), so unless I'm missing something
that one won't work either.

...And now I'm out of ideas :)  The only option I can see left is to
remove all N copies of the object and re-insert N-1 of them.
Any suggestions will be gratefully received.  If the answer is "you
can't do it", might I suggest adding a select_delete/3, in the same
style as select/3?

Cheers,

Bernard



More information about the erlang-questions mailing list