Leo-Nogismus

Leo-Nogismus

Return variables and their matches in ets select

I was wondering if there is a way to return both the variables (e.g., :“$1”), and its matched value with a single call to :ets.select(table, match_spec).

Assuming, for example, that we have an ets table called :foo, which is filled with entries of, for example, {:a, %{b: :c}}, and a match specification like [{{:a, %{b: :"$1"}, [], [something_here]}]. Is there a way to define some functions in the match specification’s body such that I get a map of “variable-atom” to “matched-value”, or similar?

So, for example:

iex(1)> :ets.new(:foo, [:bag, :public, :named_table])
iex(2)> :ets.insert(:foo, {:a, %{b: :c}})
iex(3)> :ets.select(:foo, [{{:a, %{b: :"$1"}}, [], [:"$1"]}])

returns

[:c]

but what I’m hoping to find is a way to have :ets.select return for this example

%{"$1": :c}

or anything like that.

I know that there is the match body variable :"$$" that returns a list of all the variables’ values. The issue is just that the match_spec is dynamically created, and I don’t know which variables (i.e., which integer numbers after the dollar sign) are in the match spec at the time of the select call. I need those to construct guards for a second select call.

Of course, this is a very small example. In my case, the terms stored in the ets table are much more complex, making it very hard to iterate over them to check for variable-value matches.

Any help would be appreciated.

First Post!

mudasobwa

mudasobwa

Creator of Cure

The last element in MatchSpec might be whatever, so you might go with something like

iex|💧|1 ▸ :ets.select(:foo, [{{:a, %{b: :"$1"}}, [], [{{:foo, :"$1"}}]}])
[foo: :c]

if I understood what you are after properly.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement