【问题标题】:How to update a number inside a tuple stored in an ets table?如何更新存储在 ets 表中的元组中的数字?
【发布时间】:2018-06-09 21:23:38
【问题描述】:

假设我有一个像这样的 ets 表:

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

我想使用ets:update_counter 更新元素4

我尝试了不同的方法,但找不到解决方案,例如:

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})

我希望得到如下结果:

{10,{10, 5 ,"description"}

【问题讨论】:

    标签: erlang ets


    【解决方案1】:

    我建议只对键和值使用一个元组,而不是对另一个元组中的值使用一个元组:

    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"}]
    

    【讨论】:

    • 我知道,我正在寻找如何解决我的问题。谢谢顺便说一句
    • @J.R.如果您真的需要使用update_counter,您将需要展平您的元组,即值部分不能是元组。 update_counter 不支持。假设值元组中的键和第一个位置始终相同,您可以添加包装函数以插入/读取 ets 表。插入只会插入值部分,而读取可以创建您今天拥有的嵌套元组。
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2015-02-17
    • 2014-11-30
    • 1970-01-01
    • 2022-12-06
    • 2019-09-10
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多