ETS tuple in body

Jacob jacob01@REDACTED
Fri Jan 10 17:05:49 CET 2020


Hi,

On 1/10/20 4:37 PM, Roberto Ostinelli wrote:
>     MatchBody = case is_tuple(Name) of
>         true -> {{{Name}, '$2', '$3', '$4'}};
>         _ -> {{Name, '$2', '$3', '$4'}}
>     end,
>     case ets:select(my_table, [{
>         {Name, '$2', '$3', '$4', '_', '_'},
>         [],
>         [MatchBody]
>     }])
>
> I need to do this because to make it work for Name values of both test
> and {test}.
> Is there a better way?

yes, by using a 'const' expression that will prevent variable substition
and function (tuple) evaluation in the sub-expression (here Name):

    case ets:select(my_table, [{
        {Name, '$2', '$3', '$4', '_', '_'},
        [],
        [{{const, Name}, '$2', '$3', '$4'}]
    }])

I assume there aren't nested tuples in Name, but just using {Name} like
above would only prevent the outmost tuple from being interpreted as a
"function" or variable. The 'const' expression is much safer here, since
it just says: "include as is". See the ms_transform manpage for details.

Jacob



More information about the erlang-questions mailing list