Erlang/OTP Forums

Author Message

<  Erlang  ~  Fighting hard to understand Binaris, Matching and QLC...

seancharles
Posted: Tue Nov 27, 2007 8:50 pm Reply with quote
User Joined: 18 Jul 2007 Posts: 57
There *must* be a neater and more Erlang like way to do this,
Code:

all_tags(CloudName) when is_atom(CloudName) ->
    Str = atom_to_list(CloudName),
    Len = length(Str),
    do( qlc:q([ R || R <- mnesia:table(tagcloud),
                     lists:sublist(binary_to_list(R#tagcloud.key),Len) =:= Str ])).


-record(
   tagcloud,
   { key,                                 
     frequency = 1,                                                                                 
     modified  = now()})

The key is a composite of the tag name and term eg, in the tag cloud for search terms, the word 'binary' would be expressed as:

<<"searchbinary">>

and what I want to get back is the list of all terms that 'start with <<"binary">>' but I can't seem to find a better way!

If I try to use something like this inside the qlc expression:
Code:

[ R || R <- mnesia:table(tagcloud),
       << Str, Term/binary >> =:= R#tagcloud.key]

I get told that Term is unbound, I guess I need more experience and flying time but some pointers would be good!

For now it does what I need but it just doesn't feel 'beautiful' like the book says to go for!
Smile
Sean
View user's profile Send private message
Jan Henry Nystrom
Posted: Fri Nov 30, 2007 11:09 am Reply with quote
User Joined: 09 Oct 2006 Posts: 28 Location: Uppsala, Sweden
How about:
Code:

all_tags(CloudName) when is_atom(CloudName) ->
  Key = atom_to_list(CloudName),
  Len = length(Key),
  do(qlc:q([R || R <- mnesia:table(tagcloud),
            binary_to_list(R#tagcloud.key, 1, Len) =:=
            Key])).




seancharles wrote:
There *must* be a neater and more Erlang like way to do this,
[code]
all_tags(CloudName) when is_atom(CloudName) ->
Str = atom_to_list(CloudName),
Len = length(Str),
do( qlc:q([ R || R <- mnesia:table(tagcloud),
lists:sublist(binary_to_list(R#tagcloud.key),Len) =:= Str ])).

[snip]

For now it does what I need but it just doesn't feel 'beautiful' like the book says to go for!
Smile
Sean


/Cheers Henry

_________________
Jan Henry Nystrom
Training & Research Manager @ Erlang Training and Consulting Ltd
http://www.erlang-consulting.com
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger ICQ Number
seancharles
Posted: Fri Nov 30, 2007 11:28 am Reply with quote
User Joined: 18 Jul 2007 Posts: 57
Henry,

Thanks for that. Once again I cannot believe I failed to see that form for binary_to_list!!!

That is a lot better than what I had and I thank you for opening my eyes a little more on the existing API.

However, what I was really after was something along the lines of:
<< CloudName:Len/binary, Rest/binary >>
in there somewhere!!! I thought you could use that form to split a binary, Len would be 8*size(CloudName). Have I misunderstood on that score I wonder ?

Your version is a definite improvement on mine but I wanted to understand the binary pattern bit-matching idioms a little more than I do.

Thanks again
Sean.
View user's profile Send private message
francesco
Posted: Sun Dec 02, 2007 9:13 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Sean, nice to see you around. We wondered where you were Smile I will wait for Jan - Henry to reply to your questions, but I assume that his response has put you in the right direction for the other QLC query you had?

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
seancharles
Posted: Mon Dec 03, 2007 10:14 am Reply with quote
User Joined: 18 Jul 2007 Posts: 57
Francesco,
Thanks! Very Happy

As for how I am progressing, very well! Thanks to the other helpful replies I've had, I have made leaps and bounds with my Mnesia issues.

I re-designed my strategy and now I automatically create the table on demand, with the first dirty_read() faulting to cause the table creation.

When I create a new table, I add it to a catalog and then when I start up, I 'wait' for the catalog to be loaded, then I 'wait' for all the tables named in it to be loaded... that answered my last post on knowing what tables there are.

I am sure Mnesia would list all the tables but I don't necessarily want to wait on all of them, just the ones that the tag-cloud module is dependent one so I opted to keep my own list instead.

If I had a blog I'd post about it but my own web-site http://objitsu.com is very simple as I don't have any time to fiddle with it and besides; does the world really need another blog about nothing much ?

Anyway, thanks everybody for your help. I have tried to answer a few posts myself now as I feel a bit more confident about what I am saying isn't complete crap now.


And dare I say it, having been on a few Lisp forums, this place is damned pleasant to be a member of.
Smile
Thanks
Sean

Merry Christmas / Hannuka / Thanks-Giving / etc / whatever don't let the politically correct idiots keep us down.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

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