|
|
| Author |
Message |
< Advanced Erlang/OTP ~ Mnesia QCL matching question on binaries... |
| seancharles |
Posted: Wed Nov 28, 2007 10:25 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
Can I improve the matching part of this query by using a bit-pattern matching rule, if so how because my poor old brain can't figure it out. I am referring to the lists:sublist blah blah,... converting binaries to strings just to compare the first part seems madness but I can't crack it (yet) with a binary match.
Code:
top(CloudName, N) when is_atom(CloudName), is_integer(N) ->
Str = atom_to_list(CloudName),
Len = length(Str),
mnesia:transaction(
fun()->
Cursor = qlc:cursor(
qlc:sort(
%% Sort on...
qlc:q(
%% One day I will know how to do this 'better'!
[ R#tagcloud{ key = lists:nthtail( Len, binary_to_list(R#tagcloud.key))} ||
R <- mnesia:table(tagcloud),
lists:sublist( binary_to_list(R#tagcloud.key), Len) =:= Str ]),
%% Sort options...
[{order,descending}] )),
qlc:next_answers( Cursor, N )
end).
This kind of goes with my other post, http://www.trapexit.org/forum/viewtopic.php?p=34823#34823 , but since then I have read the docs *really hard* and discovered that you CAN have differently named tables with the same record structure. I refer to the Mnsesia docs where is points out that the table def 'TabDef' is what you need and that the simplistic write/1 function needs to be replaceed with write/3 where you specify the extra information.
We shall not be beaten!
Erlang rocks and Mnsesia rolls. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|