Erlang (programming language)/Tutorials/ETS: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
(New page: =ETS local data storage= ETS is the erlang table storage system, which provides hash storage and access functions. ETS data is stored in a process as long as it is running. Here is a simp...)
 
imported>Eric Evers
Line 5: Line 5:
Here is a simple sample of how to use the most simple functions in ETS
Here is a simple sample of how to use the most simple functions in ETS


==Sample program: test_ets==
==Sample program: test_ets.erl==


  -module(test_ets).
  -module(test_ets).

Revision as of 19:33, 17 April 2008

ETS local data storage

ETS is the erlang table storage system, which provides hash storage and access functions. ETS data is stored in a process as long as it is running. Here is a simple sample of how to use the most simple functions in ETS

Sample program: test_ets.erl

-module(test_ets).
-compile(export_all).

start() -> start( mouse ).

start( Animal ) ->
	Kingdom = ets:new( 'magic',  [] ),
	% note: table is not square
	populate( Kingdom, [{micky,mouse}, {mini,mouse}, {goofy}] ),
	Member = ets:member( Kingdom, micky ),
	io:format( " ~w ~n ", [ Member ] ),
	show_next_key( Kingdom, micky ),
	find_animal( Kingdom, Animal ).
	
show_next_key( _Kingdom, '$end_of_table' ) -> done;
show_next_key( Kingdom,  Key) ->
	Next = ets:next( Kingdom, Key ),
	io:format( " next ~w ~n ", [ Next ] ),
	show_next_key( Kingdom, Next ).

populate( _Kingdom, [] ) -> {done,start};
populate( Kingdom, [H | T] ) ->
		ets:insert( Kingdom, H ),
		populate( Kingdom, T ).
	
find_animal( Kingdom, Animal ) ->
	ets:match( Kingdom, { '$1', Animal } ).
% ==============
% sample output
% ==============
% 53> test_ets:start().
% true 
%  next mini 
%  next goofy 
%  next '$end_of_table' 
%  [[mini],[micky]]