ETS tuple in body

Roberto Ostinelli ostinelli@REDACTED
Fri Jan 10 17:24:18 CET 2020


Ok I got it, your example was missing an external tuple:

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

Thank you,
r.

On Fri, Jan 10, 2020 at 5:17 PM Roberto Ostinelli <ostinelli@REDACTED>
wrote:

> Thank you Jacob,
> I already tried a couple of combinations with const but I always got an
> error message. For instance:
>
> (r@REDACTED)2> Name = test.
> test
> (r@REDACTED)3> ets:select(my_table, [{
> (r@REDACTED)3>         {Name, '$2', '$3', '$4', '_', '_'},
> (r@REDACTED)3>         [],
> (r@REDACTED)3>         [{{const, Name}, '$2', '$3', '$4'}]
> (r@REDACTED)3>     }]).
> ** exception error: bad argument
>      in function  ets:select/2
>         called as ets:select(my_table,
>                              [{{test,'$2','$3','$4','_','_'},
>                                [],
>                                [{{const,test},'$2','$3','$4'}]}])
>
>
> On Fri, Jan 10, 2020 at 5:05 PM Jacob <jacob01@REDACTED> wrote:
>
>> 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
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200110/4117537e/attachment.htm>


More information about the erlang-questions mailing list