erlangtuplesetsordered-set

Output the contents of an ETS Table Erlang


I am new to the world of Erlang so I am trying to experiment with it.

I have an ETS table that is called numbers.

ets:new(numbers,[ordered_set,named_table])

It has the format [{Name,Number},{Name,Number}] etc.

I am wondering is there a way to output the contents of the whole the ets table?


Solution

  • Tl;dr

    you can use

    ets:match_object(Tab, {'$0', '$1'}).
    

    Where Tab is your table name, i.e. numbers.

    In depth:

    The second argument is a match pattern, leaving "free variables" '$0' and '$1'.

    Let's say you inserted:

    > ets:insert(Tab, [{age, 45}, {length, 10}, {height, 45}]). 
    

    You could get out all {_, 45} tuples with:

    > ets:match_object(Tab, {'$0', 45}). 
    [{age, 45}, {height, 45}]
    

    By making all (in this case, 2) of the tuple parameters free variables, you will match everything in the table.

    I highly recommend reading Learn You Some Erlang for more info!