在 Erlang 中处理复杂的列表
Manipulating complex lists in Erlang
我在 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,
[]}]]
我正在尝试获取密钥 'affiliations' 下的用户名 (test1, testuser4_gmail.com),但无法从此列表中获取它们。
如有任何帮助,我们将不胜感激。
更新:
我很高兴获得 'affilations' 密钥及其下的所有内容,但我正在寻找的问题。我举个例子
-> Listt = [{captcha_whitelist, []}, {affiliations, [{{"test1", "54.69.16.10", []},{owner,[]}}]}].
-> lists:keyfind('affiliations',1, Listt).
Output : {affiliations,[{{"test1","54.69.16.10",[]},{owner,[]}}]}
Target : 获取像“test1”这样的值,如果那里有更多的值,比如“test1”,“testuser4_gmail.com”
根据您的代码示例,您可以通过以下方式进一步提取。
这是您的代码示例结构:
1> Listt = [{captcha_whitelist, []}, {affiliations, [{{"test1", "54.69.16.10", []},{owner,[]}}]}].
[{captcha_whitelist,[]},
{affiliations,[{{"test1","54.69.16.10",[]},{owner,[]}}]}]
我会用 proplists 提取第一层:
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"]
我在 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,
[]}]]
我正在尝试获取密钥 'affiliations' 下的用户名 (test1, testuser4_gmail.com),但无法从此列表中获取它们。
如有任何帮助,我们将不胜感激。
更新:
我很高兴获得 'affilations' 密钥及其下的所有内容,但我正在寻找的问题。我举个例子
-> Listt = [{captcha_whitelist, []}, {affiliations, [{{"test1", "54.69.16.10", []},{owner,[]}}]}].
-> lists:keyfind('affiliations',1, Listt).
Output : {affiliations,[{{"test1","54.69.16.10",[]},{owner,[]}}]}
Target : 获取像“test1”这样的值,如果那里有更多的值,比如“test1”,“testuser4_gmail.com”
根据您的代码示例,您可以通过以下方式进一步提取。
这是您的代码示例结构:
1> Listt = [{captcha_whitelist, []}, {affiliations, [{{"test1", "54.69.16.10", []},{owner,[]}}]}].
[{captcha_whitelist,[]},
{affiliations,[{{"test1","54.69.16.10",[]},{owner,[]}}]}]
我会用 proplists 提取第一层:
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"]