[erlang-questions] Nested match specifications?

Ivan Carmenates García <>
Mon Mar 4 22:53:09 CET 2013


Hi Diego,

 

I don’t know exactly if this could work for you but you can do complex
patterns like this to take what you want

 

1>  ets:new(table, [named_table]),

2>  ets:insert("Peter", [{"children", ["Bob", "Paul"]}, {"father",
["Mike"]}]}),

3>  ets:match(table, {"Peter", [{"children", '$1'},'_'] }).

[[["Bob","Paul"]]]

 

If you always match against the key, you do really faster.

 

Bests,

Ivan.

 

 

 

De: 
[mailto:] En nombre de Diego Llarrull
Enviado el: lunes, 04 de marzo de 2013 14:58
Para: 
Asunto: [erlang-questions] Nested match specifications?

 

Hello everyone, 

I would like to insert in an ETS table a tuple with the following type
signature: 

{string(), [{string(), [string()]}]}

As an example: 

{"Peter", [{"children", ["Bob", "Paul"]}, {"father", ["Mike"]}]}


My question is the following: is it possible to solve, using match
specifications, nested queries like "Retrieve the name of Peter's children"
?

That is, a query where I would need to 

a) Fetch all ("the", since its a set) tuples of size 3 where "Peter" is
located in the first position (doable with MS)
b) Let '$2' be the value in the second position of the tuple fetched in a).
Then, fetch the value corresponding to the key "children" in '$2', if
interpreted as a key-value list (i.e. lists:keyfind() should work on '$2'). 

If I understood correctly, lists:keyfind can't be used inside a match
specification because they only allow the BIFs described in
http://www.erlang.org/doc/apps/erts/match_spec.html. My question is: is
there any low-level mechanism to operate on lists inside match
specifications, or am I trying to push the boundaries of match
specifications?

In case anyone wonders "Why not use Query Lists Comprehensions?" the answer
is: "Because of performance issues: in our platform, we need to dynamically
build QLCs based on the number of arguments that arrive, which forces us to
build them as strings and then use qlc:string_to_handle() which is SLOW". 

Any help of insight will be greatly appreciated. Thank you very much in
advance. 

Diego Llarrull

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130304/a1dd84c9/attachment.html>


More information about the erlang-questions mailing list