Date range and match specification syntax

Massimo Cesaro massimo.cesaro@REDACTED
Tue Oct 7 17:17:33 CEST 2003


On Tue, 2003-10-07 at 16:51, Ulf Wiger (AL/EAB) wrote:
Hello Ulf,
> 
> On October 7, 2003, Massimo Cesaro wrote:
> >> Try to use Query = ets:fun2ms(fun(X) -> ... end) instead,
> >> slightly simpler to use :-) 
> >You kidding, don't you ?! :-)
> >Seriously, I'd like to see a short tutorial or more examples about it.
> 
> 
> Here's a short tutorial using ets, and trying to mimic your 
> problem (the ms_transform manual is daunting, I agree):
> 
> Your match spec would look pretty much like this:
> 
> fun({_,_,D}) when StartDate =< D, D =< EndDate ->
>    object()
> end
> 
I see...
> 
> 1> ets:new(cdr,[set,public,named_table,{keypos,2}]).
> cdr
> 2> [ets:insert(cdr,{cdr,D,{2003,12,D}}) || D <- lists:seq(1,31)].
> [true,
>  true|...]
> 3> ets:tab2list(cdr).
> [{cdr,28,{2003,12,28}},
>  {cdr,27,{2003,12,27}},
>  {cdr,1,{2003,12,1}},
>  {cdr,14,{2003,12|...}},
>  {cdr,5,{2003|...}},
>  {cdr,16,{...}},
>  {cdr,7|...},
>  {cdr|...},
>  {...}|...]
> 4> StartDate = {2003,12,3}.
> {2003,12,3}
> 5> EndDate = {2003,12,17}.
> {2003,12,17}
> 6> P = ets:fun2ms(fun({_,_,D}) when StartDate =< D, D =< EndDate -> object() end).
> [{{'_','_','$1'},
>   [{'=<',{const,{2003,12,3}},'$1'},{'=<','$1',{const,{2003,12,17}}}],
>   ['$_']}]
> 7> catch ets:select(cdr,P).
> [{cdr,10,{2003,12,10}},
>  {cdr,13,{2003,12,13}},
>  {cdr,15,{2003,12,15}},
>  {cdr,11,{2003,12,11}},
>  {cdr,3,{2003,12,3}},
>  {cdr,8,{2003,12,8}},
>  {cdr,12,{2003,12,12}},
>  {cdr,4,{2003,12,4}},
>  {cdr,14,{2003,12,14}},
>  {cdr,5,{2003,12,5}},
>  {cdr,16,{2003,12,16}},
>  {cdr,7,{2003,12,7}},
>  {cdr,6,{2003,12,6}},
>  {cdr,9,{2003,12,9}},
>  {cdr,17,{2003,12,17}}]
> 8>  
Well, I'm amazed, really. You managed to get in 1 line of Erlang 
a result that in other languages would require a lot more code.
Thanks for the information.
I hope I can get rid of my 10+ years of C/C++ development and start
thinking in Erlang by myself...
Cheers,
Massimo




More information about the erlang-questions mailing list