<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=us-ascii"><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:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
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;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Courier New","serif";
color:#202020;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
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 lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New","serif";color:#202020'>Thanks for the answer. As far as I understand it works great if the prefix is known at compile time. I wasn’t clear on this, but in my case the prefix is given in runtime.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New","serif";color:#202020'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New","serif";color:#202020'>So far I come up with this solution. It understands a ‘*’ character at a pattern’s end. I’d be happy to get suggestions to improve it.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New","serif";color:#202020'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>%% finds '*' in pattern and return a minimum and maximum pattern</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>%% e.g. </span><span style='font-size:10.0pt;font-family:Consolas;color:#B36A6A'><<"hello*"></span><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>> -> {</span><span style='font-size:10.0pt;font-family:Consolas;color:#B36A6A'><<"hello"></span><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>>, </span><span style='font-size:10.0pt;font-family:Consolas;color:#B36A6A'><<"hellp"></span><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>>}</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>%% if binary string X starts with 'hello' then </span><span style='font-size:10.0pt;font-family:Consolas;color:#B36A6A'><<"hello"></span><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>> </span><span style='font-size:10.0pt;font-family:Consolas;color:#B36A6A'><= X < <<"hellp"></span><span style='font-size:10.0pt;font-family:Consolas;color:firebrick'>></span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'>wildcard(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>) </span><b><span style='font-size:10.0pt;font-family:Consolas;color:#A020F0'>when</span></b><span style='font-size:10.0pt;font-family:Consolas;color:black'> is_binary(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>) </span><span style='font-size:10.0pt;font-family:Consolas;color:blue'>-></span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><b><span style='font-size:10.0pt;font-family:Consolas;color:#A020F0'>case</span></b><span style='font-size:10.0pt;font-family:Consolas;color:black'> binary:match(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, <<</span><span style='font-size:10.0pt;font-family:Consolas;color:rosybrown'>"*"</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>>>) </span><b><span style='font-size:10.0pt;font-family:Consolas;color:#A020F0'>of</span></b><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> nomatch </span><span style='font-size:10.0pt;font-family:Consolas;color:blue'>-></span><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> {</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>};</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> {</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>0</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>_</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>} </span><span style='font-size:10.0pt;font-family:Consolas;color:blue'>-></span><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> {error, wrong_pattern};</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> {</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>S</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>_</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>} </span><span style='font-size:10.0pt;font-family:Consolas;color:blue'>-></span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatPref</span><span style='font-size:10.0pt;font-family:Consolas;color:black'> = binary:part(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>0</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>S</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>-</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>1</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>),</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> io:format(</span><span style='font-size:10.0pt;font-family:Consolas;color:rosybrown'>"<b>~s</b>"</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, [</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatPref</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>]),</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> <<</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatLast</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>:</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>8</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>>> = binary:part(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pattern</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>S</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>-</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>1</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>1</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>),</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> {<<</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatPref</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>/binary, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatLast</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>:</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>8</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>>>, <<</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatPref</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>/binary, (</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatLast</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>+</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>1</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>):</span><span style='font-size:10.0pt;font-family:Consolas;color:#5A5AB4'>8</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>>>}</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><b><span style='font-size:10.0pt;font-family:Consolas;color:#A020F0'>end</span></b><span style='font-size:10.0pt;font-family:Consolas;color:black'>.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'>find(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pat</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>) </span><span style='font-size:10.0pt;font-family:Consolas;color:blue'>-></span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> {</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatMin</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatMax</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>} = wildcard(</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Pat</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>),</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>F</span><span style='font-size:10.0pt;font-family:Consolas;color:black'> = </span><b><span style='font-size:10.0pt;font-family:Consolas;color:#A020F0'>fun</span></b><span style='font-size:10.0pt;font-family:Consolas;color:black'>() </span><span style='font-size:10.0pt;font-family:Consolas;color:blue'>-></span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> qlc:eval(qlc:q(</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> [</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Patient</span><span style='font-size:10.0pt;font-family:Consolas;color:black'> || #patient{search_name = </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>N</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>}=</span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>Patient</span><span style='font-size:10.0pt;font-family:Consolas;color:black'> <- mnesia:table(pat),</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>N</span><span style='font-size:10.0pt;font-family:Consolas;color:black'> >= </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatMin</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>, </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>N</span><span style='font-size:10.0pt;font-family:Consolas;color:black'> < </span><span style='font-size:10.0pt;font-family:Consolas;color:darkgoldenrod'>PatMax</span><span style='font-size:10.0pt;font-family:Consolas;color:black'>]</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;color:black'> ))</span><span style='font-size:10.0pt;font-family:Consolas'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'> </span><b><span style='font-size:10.0pt;font-family:Consolas;color:#A020F0'>end</span></b><span style='font-size:10.0pt;font-family:Consolas;color:black'>,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'> mnesia:activity(transaction, F, [], mnesia_frag).<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'>Regards,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:Consolas;color:black'>Janos<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New","serif";color:#202020'><o:p> </o:p></span></p><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> karol skocik [mailto:karol.skocik@gmail.com] <br><b>Sent:</b> Tuesday, May 14, 2013 12:32 PM<br><b>To:</b> Janos Hary<br><b>Cc:</b> Erlang Questions<br><b>Subject:</b> Re: [erlang-questions] Mnesia wildcard query<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>You can use ets:fun2ms/1 to construct example match spec, which can be also used for select over mnesia table:<o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><p class=MsoNormal>3> Tab = ets:new(tab, []).<o:p></o:p></p></div><div><p class=MsoNormal>20496<o:p></o:p></p></div><div><p class=MsoNormal>4> ets:insert(Tab, [{"abcdef", 1}, {"abxxxxx", 2}, {"bbbbbb", 3}]).<o:p></o:p></p></div><div><p class=MsoNormal>true<o:p></o:p></p></div><div><p class=MsoNormal>5> MatchSpec = ets:fun2ms(fun ({[$a, $b | _], V}) -> V end).<o:p></o:p></p></div><div><p class=MsoNormal>[{{[97,98|'_'],'$1'},[],['$1']}]<o:p></o:p></p></div><div><p class=MsoNormal>6> ets:select(Tab, MatchSpec).<o:p></o:p></p></div><div><p class=MsoNormal>[1,2]<o:p></o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>ets:fun2ms/1 does not recognize literal funs like: fun ({"prefix" ++ _, V) -> V end, (++ is the problem)<o:p></o:p></p></div><div><p class=MsoNormal>so it's more convenient to write a simple wrapper function returning the MatchSpec, given the prefix.<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><div><p class=MsoNormal>Cheers, <o:p></o:p></p></div><div><p class=MsoNormal> Karol<o:p></o:p></p></div></div><p class=MsoNormal style='margin-bottom:12.0pt'><o:p> </o:p></p><div><p class=MsoNormal>On Tue, May 14, 2013 at 10:44 AM, Janos Hary <<a href="mailto:janos.n.hary@gmail.com" target="_blank">janos.n.hary@gmail.com</a>> wrote:<o:p></o:p></p><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=HU style='font-size:10.0pt;font-family:"Courier New","serif"'>Hi,</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=HU style='font-size:10.0pt;font-family:"Courier New","serif"'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=HU style='font-size:10.0pt;font-family:"Courier New","serif"'>Is there a way to specify a </span><span style='font-size:10.0pt;font-family:"Courier New","serif"'>‘begins with’ style query in Mnesia? My keys are strings, and I’d like to get all of them sharing a given prefix.</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Courier New","serif"'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Courier New","serif"'>Thanks,</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.0pt;font-family:"Courier New","serif"'>Janos</span><o:p></o:p></p></div></div><p class=MsoNormal style='margin-bottom:12.0pt'><br>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div></div></body></html>