Created
January 9, 2017 07:59
-
-
Save khanhhua/cd83e823dab86787c043b80faea8b1e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Open a new table setting Name as primary key in tuples of {user, Name, Age} | |
{ok, Tab} = dets:open_file('demo.dat', [{type,set}, {keypos,2}]). | |
%% Inspect table info | |
dets:info(Tab). | |
dets:info(Tab, size). | |
%% Insert user Tom with ages 30 and 40 | |
ok = dets:insert(Tab, {user, "Tom", 30}). | |
false = dets:insert_new(Tab, {user, "Tom", 40}). | |
%% .. Previous Tom will be overridden | |
ok = dets:insert(Tab, {user, "Tom", 40}). | |
%% List all items and project all attributes | |
dets:foldl(fun (X, L) -> [X|L] end, [], Tab). | |
dets:select(Tab, [{{user,'$1', '$2'},[],['$_']}]). | |
%% Selection and customize projection | |
%% - Select user "Tom" and project all attributes | |
dets:select(Tab, [{{user,'$1','$2'},[{'==','$1',"Tom"}],['$_']}]). | |
dets:select(Tab, ets:fun2ms(fun ({user, Name, Age}) -> {Name,Age} end)). | |
%% - Select user "Tom" and project Name attribute | |
dets:select(Tab, [{{user,'$1','$2'},[{'==','$1',"Tom"}],['$1']}]). | |
dets:select(Tab, ets:fun2ms(fun ({user, Name}) -> Name end)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment