Get all objects from ets table

Background

I have an ets table with several objects in it. I want to, with 1 single operation, retrieve all keys and objects my table has.

Using match

I tried using match like this:

table = :ets.new(:table, [:public])
:ets.insert(table, [{:a, 0}, {:b, 1}, {:c, 2}])
:ets.match(table, '$1')

However it always returns empty.

What am I doing wrong?

1 Like

@Fl4m3Ph03n1x :ets.tab2list is the best way to dump a whole table. Setting that aside though, is the code you posted supposed to show the issue? You aren’t inserting anything into the table.

7 Likes

Thanks for the answer !

(also, fixed the bug, thanks for spotting it, my bad!)

1 Like

for anyone (like me) wondering how this is done with :dets

:dets.foldl(fn elem, acc -> [elem | acc] end, [], t)
7 Likes

Btw, your mistake was that it is:

:ets.match(table, :"$1")`
3 Likes