从列表中提取元素在erlang中转换为字符串形式

时间:2015-04-08 10:36:07

标签: string parsing erlang ejabberd mnesia

我在mnesia数据库中有一个group_info_db表。它包含有关group的数据。它的属性是groupId,Grouptype,GroupName,xxx,xxx,members.This成员属性包含字符串形式的所有成员的名称(Ex-& #34; + 1 @ devlab,+ 1111 @ devlab,+ 919470220061 @ devlab,+ 111 @ devlab,")。我获取了对成员进行攻击的所有组。有时一个成员与许多组关联,所以a从db.Like中获取了大量数据

  [{group_info_db,"12err1","0","Atif_aslam2","+111@devlab","+111@devlab","+1@devlab,    +1111@devlab,+919470220061@devlab,+111@devlab,"},
{group_info_db,"GroupID12","1","GroupName12","+3@devlab","+3@devlab",
"+1@devlab,+2@devlab,+3@devlab,+918800869860@devlab,+3@devlab,"},
{group_info_db,"VVv","Viv_12345","0","+111@devlab","+111@devlab",
"+1@devlab,+11@devlab,+111@devlab,"},
{group_info_db,"VVV5","0","Atif_aslam2","+111@devlab","+111@devlab",
"+1@devlab,+1111@devlab,+919470220061@devlab,+111@devlab,"},
{group_info_db,"VVV6","0","Atif_aslam2","+111@devlab","+111@devlab",
"+1@devlab,+1111@devlab,+919470220061@devlab,+111@devlab,"},
{group_info_db,"GroupID","GroupName","GroupType","+3@devlab","+3@devlab",
"+1@devlab,+2@devlab,+3@devlab,+3@devlab,"},
{group_info_db,"12err","0","Viv123","+111@devlab","+111@devlab",
"+1@devlab,+1111@devlab,+919470220061@devlab,+111@devlab,"},
{group_info_db,"12er","0","At","+111@devlab","+111@devlab",
"+1@devlab,+1111@devlab,+919470220061@devlab,+111@devlab,"},
{group_info_db,"12e","0","Vi123","+111@devlab","+111@devlab",
"+1@devlab,+1111@devlab,+919470220061@devlab,+111@devlab,"}]

我的问题是我还需要该组与此Fetched数据关联的所有Id组(第一个属性)。我将此Fetched数据转换为字符串a然后尝试将其解析为ferch所有groupids,但是很长的方法。还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

最简单的方法可能只是降低组列表并获取每个组元组的第二个元素:

get_group_id([Group|Groups]) ->
    [element(2, Group)|get_group_id(Groups)];
get_group_id([]) -> [].

看到每个组元组都有:

{group_info_db,GroupId,GroupType,GroupName,AdminId,CreatorId,Members}

您也可以使用模式匹配直接编写函数:

get_group_id([{group_info_db,GroupId,_,_,_,_,_}|Groups]) ->
    [GroupId|get_group_id(Groups)];
get_group_id([]) -> [].

或使用列表:map / 2功能:

get_group_id(Groups) ->
    lists:map(fun ({group_info_db,GroupId,_,_,_,_,_}) -> GroupId end, Groups).

如果不是将成员生成为一个长字符串而是生成一个成员名称字符串列表,那么它将更容易使用。这样组信息就会变成:

{group_info_id,"12err1","0","Atif_aslam2","+111@devlab","+111@devlab",
 ["+1@devlab","+1111@devlab","+919470220061@devlab","+111@devlab"]}

它是什么类型的信息?