6

Suppose I have an ets table like:

I = ets:new(mytable, [named_table, set]).
ets:insert(I, {10,{10, 4 ,"description"}).

I would like to update the element 4 using the ets:update_counter.

I tried in different way, but can't find the solution, for example:

ets:update_counter(I, 10 , {3,1}).

** exception error: bad argument
     in function  ets:update_counter/3
        called as ets:update_counter(mytable,10,{3,1})

I'd like to have the result as:

{10,{10, 5 ,"description"}
2240
  • 1,547
  • 2
  • 12
  • 30
J.R.
  • 2,335
  • 2
  • 19
  • 21

1 Answers1

5

I recommend to use just one tuple for key and values, instead of using a tuple for values in another tuple:

1> I = ets:new(mytable, [named_table, set]).
mytable
2> ets:insert(I, {10, 10, 4 ,"description"}).
true
3> ets:update_counter(I, 10 , {3,1}).        
5
4> ets:lookup(I, 10).
[{10,10,5,"description"}]
Pouriya
  • 1,626
  • 11
  • 19
  • I know that, I was looking how to solve my problem. Thanks btw – J.R. Dec 29 '17 at 16:42
  • 2
    @J.R. If you really need to use `update_counter` you will need to flatten your tuple, i.e. the value part can't be a tuple. `update_counter` doesn't support that. Assuming that the key and the first position in the value tuple is always the same you could add wrapper functions for inserting/reading to/from the ets table. The insert would just insert the value part and the read could create the nested tuple that you have today. – johlo Dec 29 '17 at 22:42