[erlang-questions] Help on dictionary search

Christian S chsu79@REDACTED
Tue Nov 7 15:45:16 CET 2006


On 11/7/06, Bob Cowdery <Bob.Cowdery@REDACTED> wrote:
> I have a dictionary of the form [{class, [Pid, Pid, Pid ...]}, {class1[...]}, ...]. A given Pid can appear in one or more classes. Given a Pid I want to delete all instances of that Pid from the dictionary and if the class then has an empty list to remove the class. Generally I look up the dictionary by class and don't mind a linear search for the delete as its occational.

A suggestion, use the 'sets' module (or at least 'ordsets' if the list
representation is important).

remove_pid(Classes, Pid) ->
  Classes1 = [{Class, sets:del_element(Pid, Set)} || {Class, Set} <- Classes],
  Classes2 = [{Class, Set} || {Class, Set} <- Classes1, sets:size(Set) =/= 0].

[also untested]



More information about the erlang-questions mailing list