[erlang-questions] searching in an ETS table for substring

Avinash Dhumane <>
Sat Feb 11 14:28:58 CET 2012


I am learning Erlang, and I must be doing something wrong here; please correct me! Thanks.


:~$ erl
Erlang R14B02 (erts-5.8.3) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.8.3  (abort with ^G)
1>  T=ets:new(test, [set]).
16400
2>  ets:insert(T, {"erlang", 1}).
true
3>  ets:insert(T, {"ericsson", 2}).
true
4> ets:insert(T, {"erring", 3}).   
true
5>   
5> ets:insert(T, {"ejection", 4}).
true
6>  ets:insert(T, {"emersion", 5}).
true
7> ets:select(T, [{{'$1', '_'}, [], ['$$']}]).
[["erring"],
 ["emersion"],
 ["ericsson"],
 ["erlang"],
 ["ejection"]]
8> ets:select(T, [{{'$1', '_'}, [{'==', '$1', "er"++'_'}], ['$$']}]).
[]
9> ets:select(T, [{{'$1', '_'}, [{'=:=', '$1', "er"++'_'}], ['$$']}]).
[]
10> ets:select(T, [{{'$1', '_'}, [{'=', '$1', "er"++'_'}], ['$$']}]).  
** exception error: bad argument
     in function  ets:select/2
        called as ets:select(16400,
                             [{{'$1','_'},[{'=','$1',[101,114|'_']}],['$$']}])
11> 




________________________________
 From: Ulf Wiger <>
To: Martin Dimitrov <> 
Cc: Erlang Questions <> 
Sent: Thursday, 9 February 2012 9:18 PM
Subject: Re: [erlang-questions] searching in an ETS table for substring
 

If the key is a list (not a binary), you can use the pattern "foo" ++ '_'.

There is no equivalent syntax for binaries.

If the table is an ordered set, this search will be efficient, since it will use the bound prefix to narrow the search.

BR,
Ulf

On 9 Feb 2012, at 16:37, Martin Dimitrov wrote:

> Hello,
> 
> Is it possible to search in an ETS table for a key that starts with
> certain characters?
> Something equivalent to:
> 
> SELECT * FROM table WHERE name LIKE 'some%'
> 
> I read the documentation for select/2 and it says we can use as guards
> functions as is_* and the like.
> 
> Best regards,
> 
> Martin
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120211/18cec851/attachment.html>


More information about the erlang-questions mailing list