ERLANG 将字符串转换为元组列表

ERLANG Convert String to List of Tuples

我有一串用户名,形式为 "hello1@devlab, hello2@devlab, hello3@devlab" 此字符串可以有 N 个用户名。

我想将其转换为以下形式的数据包:

Packet_in_tuple_form={xmlel,<<"message">>,[{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"ABCMSG">>}]},{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello1@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello2@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello3@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]}
]
}]},

因此,通过添加额外的 xmlel 元组,此数据包应该能够容纳 N 个用户名 [根据字符串中的用户名数]:-

Packet_in_tuple_form={xmlel,<<"message">>,[{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"ABCMSG">>}]},{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello1@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello2@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello3@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello4@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello5@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]}
]
}]},

我怎样才能做到这一点?

感谢和问候

这是一个如何将字符串转换为地址列表的示例:

Str = "hello1@devlab, hello2@devlab, hello3@devlab",
Addrs = [I || I <- string:tokens(Str, ", ")].

因此可以从 Addrs 生成数据包的地址列表,如下所示:

[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,list_to_binary(JID)},{<<"desc">>,<<"description goes here!">>}],[]} || JID <- Addrs].