Need help with :ets.match - I always end up with an empty list []

There’s an unfortunate overlap in terminology: :ets.match takes a “match pattern”, not a match specification. The documentation for :ets.select/2 demonstrates this:

ets:match(Table,{'$1','$2','$3'})

# is equivalent to

ets:select(Table,[{{'$1','$2','$3'},[],['$$']}])

Demo:

iex(1)> tab = :ets.new(:foo, [:ordered_set])
#Reference<0.3893230368.2635726851.91982>

iex(3)> :ets.insert(tab, {{"a", 1, "b"}, "c"})
true

iex(4)> :ets.insert(tab, {{"d", 2, "e"}, "f"})
true

iex(6)> :ets.select(tab, [{{{:"$1", :"$2", :"$3"}, :"$4"}, [{:<, :"$2", 2}], [:"$_"]}])
[{{"a", 1, "b"}, "c"}]
2 Likes