# 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,[]).
11
2> ets:insert(T,[{[a,b,c,d],1},{[a,b,c],2},{[a,b],3}]).
true

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

3> ets:match(T,{[a|'\$1'],'\$2'}).
[[[b,c,d],1],[[b],3],[[b,c],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'}).
[[[c,d],1],[[],3],[[c],2]]

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

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

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

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

It seems to work fine.

Regards,
*Erik.

```

More information about the erlang-questions mailing list