[erlang-questions] Help on dictionary search
Bob Cowdery
Bob.Cowdery@REDACTED
Tue Nov 7 15:40:53 CET 2006
Serge
Thank you very much. It seems to work, which I guess would be no great surprise to you. I will figure out how later. You managed to reply before I even saw my message reflected, that's what I call service!
Bob
Try this (from problem statement I assume by a dictionary you mean a
list rather than the dict module):
remove_pid(Pid, Dict) ->
remove_pid(Pid, Dict, []).
remove_pid(_Pid, _Dict = [], Acc) ->
lists:reverse(Acc);
remove_pid(Pid, [{Class, List} | Tail], Acc) ->
case [P || P <- List, P =/= Pid] of
[] ->
remove_pid(Pid, Tail, Acc);
L ->
remove_pid(Pid, Tail, [{Class, L} | Acc])
end.
[Not tested].
Bob Cowdery wrote:
> I wonder if someone could help me with what I guess must seem a fairly elementary problem.
>
> 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.
>
> I think it's something like this, but I get lost on the details and don't know how to 'do something' with the head. Do I need to return a new dictionary.
>
> search({_,[_, Pid, _]}, H|T) ->
> %% we have a match of the Pid
> %% do something with it!
> search({_,[_, Pid, _]}, _|T) ->
> %% no match so recurse with the rest
> search ({_,[_, Pid, _]}, T);
> search({_,[_, Pid, _]}, []) ->
> ok
>
> Thanks
> Bob
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list