Erlang ets:select sublist

292 views Asked by At

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!!

1

There are 1 answers

3
choroba On

It's not possible. You need to design your data structure to be searchable by the guard expressions, for example:


-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, _ = '_'}, _ = '_'},
                 [],
                 ['$_']
                }]).