<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";
        color:black;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML con formato previo Car";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";
        color:black;}
tt
        {mso-style-priority:99;
        font-family:"Courier New";}
span.HTMLconformatoprevioCar
        {mso-style-name:"HTML con formato previo Car";
        mso-style-priority:99;
        mso-style-link:"HTML con formato previo";
        font-family:Consolas;
        color:black;}
span.EstiloCorreo20
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 3.0cm 70.85pt 3.0cm;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body bgcolor=white lang=ES link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Hi Diego,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I think both functions are the same about performance, I think the questions is of how to use it, I bet you will get the same result using the function you like most while you reduce the complexity of the query by reducing calls to other functions and the times you filter the data, and as last point while more specific you come getting the data, using keys, or indexers in the query.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>It’s my placer to help, that’s why we are here I think.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Best, Ivan.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext'>De:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext'> erlang-questions-bounces@erlang.org [mailto:erlang-questions-bounces@erlang.org] <b>En nombre de </b>Diego Llarrull<br><b>Enviado el:</b> martes, 05 de marzo de 2013 11:22<br><b>Para:</b> erlang-questions@erlang.org<br><b>Asunto:</b> Re: [erlang-questions] Nested match specifications?<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>Ivan, Patrik:<br><br>Thank you both for the swift replies! Both provided functions that yield the desired results. The functions are: <br><br><tt><span lang=EN-US style='font-size:10.0pt'> (I) ets:match(table, {"Peter", [{"children", '$1'},'_'] }).                         (Ivan's)</span></tt><span style='font-size:10.0pt;font-family:"Courier New"'><br><tt>(II) ets:select(xxx,ets:fun2ms(fun({"Peter",[{"children",Data}|_]}) -> Data end)).   (Patrick's)</tt></span><br><br>(II) is effectively rendered to<br><br><tt><span style='font-size:10.0pt'>ets:select(xxx,[{{"Peter",{"children",'$1'}},[],['$1']}])</span></tt><br><br>which is pretty similar to Ivan's suggestion. My final question (and I so promise) is: it appears to me that (I) performs better than (II) because of the lack of need of a transformation function such as ets:fun2ms(). However, I don't known for sure whether ets:match() outperforms ets:select() or not. I'm about to run a series of tests on big datasets to check this, but I imagine that there is already an answer to this question. <br><br>Once again, thank you very much. <br><br>Diego<br><br><br>El 05/03/13 11:34, Patrik Nyblom escribió:<o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal>Hi!<br>On 03/04/2013 08:58 PM, Diego Llarrull wrote:<o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><p class=MsoNormal style='margin-bottom:12.0pt'>Hello everyone, <br><br>I would like to insert in an ETS table a tuple with the following type signature: <br><br><tt><i><span style='font-size:10.0pt'>{string(), [{string(), [string()]}]}</span></i></tt><br><br>As an example: <br><i><br></i><tt><i><span style='font-size:10.0pt'>{"Peter", [{"children", ["Bob", "Paul"]}, {"father", ["Mike"]}]}</span></i></tt><i><br><br></i><br>My question is the following: is it possible to solve, using match specifications, nested queries like "Retrieve the name of Peter's children" ?<br><br>That is, a query where I would need to <br><br>a) Fetch all ("the", since its a set) tuples of size 3 where "Peter" is located in the first position (doable with MS)<br>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'). <br><br>If I understood correctly, lists:keyfind can't be used inside a match specification because they only allow the BIFs described in <a href="http://www.erlang.org/doc/apps/erts/match_spec.html">http://www.erlang.org/doc/apps/erts/match_spec.html</a>. 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?<o:p></o:p></p></blockquote><p class=MsoNormal>Yes, you're trying to push the boundaries of match specifications :)<br><br>You'll have to do a lists:keysearch/keyfind on the results of the ets:select (when you're back in proper Erlang code), only constant time BIF's can be added to the ms language, so it's unfortunately not even an option to add this particular function... Without a huge rewrite that is... <br><br>So...<br>  [lists:keyfind("children",1,P) || P <- ets:select(xxx,ets:fun2ms(fun({"Peter",Data}) -> Data end))].<br>would be the solution. Except of course if the list has a particular order, or you instead use some record'ish data structure, so that you can match directly:<br>  ets:select(xxx,ets:fun2ms(fun({"Peter",[{"children",Data}|_]}) -> Data end)).<br>But looping over the list inside the ms is not possible.<br><br><o:p></o:p></p><p class=MsoNormal>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". <br><br>Any help of insight will be greatly appreciated. Thank you very much in advance. <br><br>Diego Llarrull<br><br>  <o:p></o:p></p><p class=MsoNormal>Cheers,<br>/Patrik<br><br><o:p></o:p></p><p class=MsoNormal><br><br><br><br><o:p></o:p></p><pre>_______________________________________________<o:p></o:p></pre><pre>erlang-questions mailing list<o:p></o:p></pre><pre><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><o:p></o:p></pre><pre><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><o:p></o:p></pre><p class=MsoNormal><br><br><br><br><o:p></o:p></p><pre>_______________________________________________<o:p></o:p></pre><pre>erlang-questions mailing list<o:p></o:p></pre><pre><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><o:p></o:p></pre><pre><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><o:p></o:p></pre></blockquote><p class=MsoNormal><o:p> </o:p></p></div></body></html>