billc
11/30/2017 - 6:04 PM

DETS Select Statement with a Compound Key

Match expression allows you to retrieve a partial key or specific value from a DETS table.

:dets.insert(file, { {"graw", "titanai"}, 2, "tennessee" })
:dets.insert(file, { {"graw", "grendel"}, 1, "canton" })
:dets.insert(file, {{"gta5", "grendel"}, 1, "atlanta"})

p = "grendel"

:dets.select(file, [{{{:"$1", :"$2"}, :"$3", :"$4"}, [{:"=:=", {:const, p}, :"$2"}],[{{:"$1", "grendel", :"$3", :"$4"}}]}])
## [{"graw", "grendel", 1, "canton"}, {"gta5", "grendel", 1, "atlanta"}]

# or use :ets.fun2ms to build the match expression but does not work on dynamic values
# the value is interpetted at execution time for the match conversion
fun = :ets.fun2ms(fn {{game, ^p}, id, loc} -> {game, "grendel", id, loc} end)
:dets.select(file, fun)