Erlang ets:选择子列表

50few1ms  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(165)

Is there a way in Erlang to create a select query on ets table, which will get all the elements that contains the searched text?

ets:select(Table,
  [{ %% Match spec for select query
      {'_', #movie_data{genre = "Drama" ++ '_' , _ = '_'}},  % Match pattern
      [],                                                    % Guard
      ['$_']                                                 % Result
  }]) ;

This code gives me only the data that started (=prefix) with the required text (text = "Drama"), but the problem is I need also the the results that contain the data, like this example:

movie_data{genre = "Action, Drama" }

I tried to change the guard to something like that -
{'_', #movie_data{genre = '$1', _='_'}}, [string:str('$1', "Drama") > 0] ...
But the problem is that it isn't a qualified guard expression.
Thanks for the help!!

nwlls2ji

nwlls2ji1#

这是不可能的。您需要将数据结构设计为可通过保护表达式搜索,例如:

-record(movie_data, {genre, name}).
-record(genre, {comedy, drama, action}).

example() ->
    Table = ets:new('test', [{keypos,2}]),
    ets:insert(Table, #movie_data{name  = "Bean",
                                  genre = #genre{comedy = true}}),
    ets:insert(Table, #movie_data{name  = "Magnolia",
                                  genre = #genre{drama = true}}),
    ets:insert(Table, #movie_data{name  = "Fight Club",
                                  genre = #genre{drama = true, action = true}}),
    ets:select(Table,
               [{#movie_data{genre = #genre{drama = true, _ = '_'}, _ = '_'},
                 [],
                 ['$_']
                }]).

相关问题