> Erlang中文手册 > lookup_all/2 返回列表里跟键相关联的所有条目的列表

proplists:lookup_all/2

返回列表里跟键相关联的所有条目的列表

用法:

lookup_all(Key, List) -> [tuple()]

内部实现:

-spec lookup_all(Key, List) -> [tuple()] when
      Key :: term(),
      List :: [term()].

lookup_all(Key, [P | Ps]) ->
    if is_atom(P), P =:= Key ->
	    [{Key, true} | lookup_all(Key, Ps)];
       tuple_size(P) >= 1, element(1, P) =:= Key ->
	    [P | lookup_all(Key, Ps)];
       true ->
	    lookup_all(Key, Ps)
    end;
lookup_all(_Key, []) ->
    [].

返回列表 List 里跟键 Key 相关联的所有条目的列表。如果没有这样的条目存在,则返回一个空列表。

proplists:lookup_all(k1, [{k1, 1}, {k2, 2}, k1, {k3, 3}, {k4, [[4]]}]).
proplists:lookup_all(k3, [{k1, 1}, {k2, 2}, k3, {k3, 3}, {k4, [[4]]}]).
proplists:lookup_all(k5, [{k1, 1}, {k2, 2}, k3, {k3, 3}, {k4, [[4]]}]).