Advanced ETS Pattern Matching

Erik Reitsma (ELN) <>
Tue Jan 7 09:14:58 CET 2003

> Does anyone know if ETS can support pattern matching on "the > rest of a list of terms" if the key is a list of terms?

Yes, it does in the cases you describe.

> Examples:
> Given keys:
> [a,b,c,d]
> [a,b,c]
> [a,b]

Eshell V5.2  (abort with ^G)
1> T=ets:new(my_table,[]).
2> ets:insert(T,[{[a,b,c,d],1},{[a,b,c],2},{[a,b],3}]).

> Find all three with the following pattern:
> [a,'*']

3> ets:match(T,{[a|'$1'],'$2'}).

> Find the last two with this pattern:
> [a,b,'*']

If '*' means zero or more elements, it would match all keys:

4> ets:match(T,{[a,b|'$1'],'$2'}).

If it matches one or more elements, I have to add another element:

5> ets:match(T,{[a,b,'$3'|'$1'],'$2'}).

> Find [a,b] with this pattern:
> ['_',b]

6> ets:match(T,{['$1',b],'$2'}).       

It seems to work fine.


More information about the erlang-questions mailing list