[erlang-questions] Strange ets:select behaviour

Magnus Henoch magnus@REDACTED
Thu Dec 11 15:59:16 CET 2008


"Maxim Treskin" <zerthurd@REDACTED> writes:

> But with this MatchSpec I have error:
>
> (n1@REDACTED)7> ets:select(controllers, [{{{'$1', '$2', '$3'}, '$4'}, [{'==', '$1',
> {192,168,138,73}}, {is_pid, '$4'}], ['$4']}]).
> ** exception error: bad argument
>      in function  ets:select/2
>         called as ets:select(controllers,
>                              [{{{'$1','$2','$3'},'$4'},
>                                [{'==','$1',{192,168,138,73}},{is_pid,'$4'}],
>                                ['$4']}])
>
>
> Is it means that impossible to use tuples in MatchSpec guards?

Not impossible, but you have to write it as either {{192,168,138,73}} (a
nested tuple) or {const, {192,168,138,73}}.  From "Match specifications
in Erlang", http://www.erlang.org/doc/apps/erts/match_spec.html#1.3 :

  In the MatchHead part, all literals (except the variables noted above)
  are interpreted as is. In the MatchCondition/MatchBody parts, however,
  the interpretation is in some ways different. Literals in the
  MatchCondition/MatchBody can either be written as is, which works for
  all literals except tuples, or by using the special form {const, T},
  where T is any Erlang term. For tuple literals in the match_spec, one
  can also use double tuple parentheses, i.e., construct them as a tuple
  of arity one containing a single tuple, which is the one to be
  constructed. The "double tuple parenthesis" syntax is useful to
  construct tuples from already bound variables, like in {{'$1',
  [a,b,'$2']}}.

You might find it easier not to write match specifications manually,
instead using ets:fun2ms/1.

-- 
Magnus Henoch, magnus@REDACTED
Erlang Training and Consulting
http://www.erlang-consulting.com/




More information about the erlang-questions mailing list