Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a number inside a tuple stored in an ets table?

Tags:

erlang

ets

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"}
like image 230
J.R. Avatar asked Dec 29 '17 12:12

J.R.


1 Answers

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"}]
like image 76
Pouriya Avatar answered Oct 25 '22 03:10

Pouriya