gpt4 book ai didi

(Elixir/Erlang) :ets.select_count returns 0((elixir/Erlang):ets.select_count返回0)

转载 作者:bug小助手 更新时间:2023-10-24 17:54:46 27 4
gpt4 key购买 nike



I'm writing a program using Elixir and I have a very strange error.

我正在用长生不老药编写程序,我有一个非常奇怪的错误。


I have a table, which contains some values stored like that: {a, b}. A is unique value, b can be repeated.

我有一个表,其中包含一些像这样存储的值:{a,b}。A是唯一值,b可以重复。


I want to count how many rows I have, which have specific second value.

我想计算一下我有多少行,其中有特定的第二个值。


When I call

当我打电话的时候


:ets.select_count(table, [{{:'$1', "a"}, [], [:'$1']}])

it returns 0, but if I call

它返回0,但如果我调用


length(:ets.select(table, [{{:'$1', "a"}, [], [:'$1']}]))

it returns expected result.

它返回预期结果。


Table can't be changed between these two calls.

在这两次调用之间不能更改表。


Versions:

版本:



  • erlang - 25.3.2.5

  • elixir - 1.15.4
    (I took versions from package manager, tell me if it's wrong way)


I tried to find answer in google, I found nothing.

我试着在谷歌上寻找答案,但一无所获。


What am I doing wrong?

我做错了什么?


To reproduce:

复制:


table = :ets.new(:tbl, [])
:ets.insert(table, {"a", "b"})
:ets.insert(table, {"b", "a"})
IO.puts('select_count: #{:ets.select_count(table, [{{:'$1', "a"}, [], [:'$1']}])}') # prints 0
IO.puts('length(select): #{length(:ets.select(table, [{{:'$1', "a"}, [], [:'$1']}]))}') # prints 1

更多回答
优秀答案推荐

According to the Erlang docs:

根据Erlang的文件:


https://www.erlang.org/doc/man/ets#select_count-2

Https://www.erlang.org/doc/man/ets#select_count-2



This function can be described as a select_delete/2 function that does not delete any elements, but only counts them.



So look at the docs for select_delete/2:

因此,看看SELECT_DELETE/2的文档:


https://www.erlang.org/doc/man/ets#select_delete-2

Https://www.erlang.org/doc/man/ets#select_delete-2



The match specification has to return the atom true if the object is to be deleted. No other return value gets the object deleted.



So you need to do this:

因此,您需要这样做:


:ets.select_count(table, [{{:'$1', "a"}, [], [true]}])

Note the return value is true, to accord with the quote above.

请注意,返回值为TRUE,以符合上面的引用。


更多回答

Thank you very much! I have tried to read documentation, but apparently I read it inaccurately and missed this part. Thanks again

非常感谢!我试着阅读文档,但显然我读得不准确,错过了这一部分。再次感谢

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com