Date range and match specification syntax

Raimo Niskanen raimo@REDACTED
Fri Oct 10 09:05:17 CEST 2003


Try also

ets:fun2ms(fun({_,_,D}) when (StartDate =< D) or (D =< EndDate) -> 
object() end)


Ulf Wiger AL/EAB wrote:
> fun({_,_,D}) when StartDate =< D -> object();
>    ({_,_,D}) when D =< EndDate -> object()
> end.
> 
> (:
> 
> /Uffe
> 
> -----Original Message-----
> From: Torbjorn Tornkvist [mailto:tobbe@REDACTED]
> Sent: den 9 oktober 2003 13:28
> To: Ulf Wiger (AL/EAB)
> Cc: Erlang mailing list
> Subject: Re: Date range and match specification syntax
> 
> 
> Nice, but how do I express 'or', as in:
> 
> 1 > ets:fun2ms(fun({_,_,D}) when StartDate =< D; D =< EndDate -> 
> object() end).
> Error: fun with semicolon (;) in guard cannot be translated into match_spec
> {error,transform_error}
> 
> ? /Tobbe
> 
> Ulf Wiger (AL/EAB) wrote:
> 
> 
>>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
>>
>>
>>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>  
>> 
>>
> 
> 

-- 
/ Raimo Niskanen, Erlang/OTP, Ericsson AB




More information about the erlang-questions mailing list