在Erlang中操作复杂列表

时间:2015-07-30 13:18:59

标签: erlang ejabberd

我在Erlang中有这个列表

[{title,
"ad"},
{description,
"fdf"},
{allow_change_subj,
true},
{allow_query_users,
true},
{allow_private_messages,
true},
{allow_private_messages_from_visitors,
anyone},
{allow_visitor_status,
true},
{allow_visitor_nickchange,
true},
{public,
true},
{public_list,
true},
{persistent,
true},
{moderated,
true},
{members_by_default,
true},
{members_only,
false},
{allow_user_invites,
true},
{password_protected,
false},
{captcha_protected,
false},
{password,
[]},
{anonymous,
true},
{logging,
false},
{max_users,
200},
{allow_voice_requests,
true},
{voice_request_min_interval,
1800},
[{captcha_whitelist, []}, 
{affiliations, [{{"test1", "serverdomain.com", []},{owner,[]}}]}, {{"testuser4_gmail.com", "serverdomain.com", []},    {member, []}}],
{subject,
[]},
{subject_author,
[]}]]

我正在尝试获取位于关键“附属关系”下的用户名(test1,testuser4_gmail.com),但在从此列表中获取它们时遇到问题。

非常感谢任何帮助。

更新:

我很高兴得到'关系'的关键以及其中的一切,但我正在寻找的问题。我正在提供一个例子

 ->   Listt = [{captcha_whitelist, []}, {affiliations, [{{"test1", "54.69.16.10", []},{owner,[]}}]}].

  ->  lists:keyfind('affiliations',1, Listt).
  Output : {affiliations,[{{"test1","54.69.16.10",[]},{owner,[]}}]}

目标获取“test1”之类的值,如果可用的话更多,例如“test1”,“testuser4_gmail.com”

1 个答案:

答案 0 :(得分:2)

根据您的代码示例,以下是您可以进一步提取的一些方法。

以下是您的代码示例结构:

1> Listt = [{captcha_whitelist, []}, {affiliations, [{{"test1", "54.69.16.10", []},{owner,[]}}]}].
[{captcha_whitelist,[]},
 {affiliations,[{{"test1","54.69.16.10",[]},{owner,[]}}]}]

我会用支持者提取第一级:

2> Affiliations =  proplists:get_value(affiliations,Listt).
[{{"test1","54.69.16.10",[]},{owner,[]}}]

lists:map/2和模式匹配可以帮助您仅使用结构的第一部分转换列表:

3> Users = lists:map(fun({User, Role}) -> User end, Affiliations).
[{"test1","54.69.16.10",[]}]

如果您想进一步提取,可以这样做:

4> lists:map(fun({Username, Server, _}) -> Username end, User). 
["test1"]

或者您甚至可以立即执行第3步和第4步并获取用户列表:

5> lists:map(fun({{User, Server, _}, Role}) -> User end, Affiliations).       
["test1"]