[erlang-questions] learning mnesia:select syntax

Jani Launonen <>
Thu Oct 4 15:47:25 CEST 2007


Richard Andrews kirjoitti 4.10.2007 kello 13.37:

> I've been studying mnesia select (and ERTS by extension) from OTP  
> docs for days
> and I am baffled. Anyone know of good material for learning this?
> Is it worth looking at the mnesia code?

I think that mnesia documentation is quite nice. Perhaps you should  
have some hands on experimenting to become more used to the select?

I've never used match_spec in my life, but I started an erlang shell,  
cut'n'pasted from http://www.erlang.org/doc/apps/mnesia/ 
Mnesia_chap5.html#5 chapter 5.10 the example tables fruit and  
vegetable to a file (fruits.txt) and loaded it from the shell by

mnesia:load_textfile("fruits.txt").

I defined the same records for shell just as a convenience

rd(fruit, {name, color, taste}).
rd(vegetable, {name, color, taste, price}).

and after some tinkering made match_specs from select example in  
mnesia documentation:
MatchHead = #fruit{name='$1', _='_', taste=sweet}.
Guards = [].
Results = ['$1'].

Then the selection:
mnesia:dirty_select(fruit, [{MatchHead, Guards, Results}]).

which returned [orange, apple] as expected.

The same with vegetable table:
MatchHead1 = #vegetable{name='$1', _='_', _='_', price='$2'}.
Guards1 = [{'>', 2.0, '$2'}].

mnesia:dirty_select(vegetable, [{MatchHead1, Guards1, Results}]).

which resulted in [potato] as expected.

Just try these and try making more compicated examples stepwisely. I  
hope that helped.

Cheers,
Jani Launonen


>
>
>
>       Sick of deleting your inbox? Yahoo!7 Mail has free unlimited  
> storage.
> http://au.docs.yahoo.com/mail/unlimitedstorage.html
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list