Inverse match on ets?

Ulf Wiger (AL/EAB) <>
Mon Oct 10 14:04:45 CEST 2005


Here's an example, but it depends a bit on how generic you want the function to be.

1> ets:new(tab, [bag,public,named_table]).
tab
2> ets:insert(tab, {{record,a,'_'},pid(0,57,0)}). 
true
3> ets:insert(tab, {{record,b,'_'},pid(0,59,0)}).
true
4> ets:insert(tab, {{record,'_',7},pid(0,62,0)}).  
true
6> F = fun({record,X,Y}) -> ets:select(tab, [{{{record,'$1','$2'},'$3'}, [{'or',{'==','$1',X},{'==','$2',Y}}], ['$3']}]) end.  
#Fun<erl_eval.6.10732646>
7> catch F({record,a,7}).
[<0.57.0>,<0.62.0>]                          


BTW, I wouldn't recommend using '_' as a regular value inside an ets table. While it works, it may complicate select patterns somewhat.

/Uffe

> -----Original Message-----
> From: 
> [mailto:]On Behalf Of Tim Bates
> Sent: den 10 oktober 2005 13:38
> To: 
> Subject: Inverse match on ets?
> 
> 
> Hi folks,
> I want to be able to store patterns in an ets table and 
> retrieve those 
> that match a given example, like this eg:
> 
> Stored in ets:
> {{record, a, '_'}, <0.57.0>}
> {{record, b, '_'}, <0.59.0>}
> {{record, '_', 7}, <0.62.0>}
> 
> get_matching_pids({record, a, 7}) -> [<0.57.0>, <0.62.0>]
> 
> Sort of like a match-spec but in reverse, where the unbound 
> elements are 
> in the table. Any suggestions on how I might implement 
> get_matching_pids/1?
> 
> Thanks,
> Tim.
> 
> -- 
> Tim Bates
> 
> 



More information about the erlang-questions mailing list