I have a group_info_db table in mnesia database.Which contain the data about group.Its attributes are groupId,Grouptype,GroupName,xxx,xxx,members.This member attribute contains name of all the members in string form(Ex-"+1@devlab,+1111@devlab,+919470220061@devlab,+111@devlab,").I fetched all the groups corresping to a member.nOW Sometimes a member is associated with many groups so a very big amount of data is fetched from 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,"}]
My problem is that i also need all the Id of group(First attribute) with which this group is associated along this Fetched data.I converted this Fetched data to string a then tried to parse it to ferch all groupids ,but that is very long method.Any other idea?
The easiest way is probably just to step down the list of groups and get the second element of every group tuple:
get_group_id([Group|Groups]) ->
[element(2, Group)|get_group_id(Groups)];
get_group_id([]) -> [].
Seeing each group tuple has the :
{group_info_db,GroupId,GroupType,GroupName,AdminId,CreatorId,Members}
you could also write the function directly using pattern matching as:
get_group_id([{group_info_db,GroupId,_,_,_,_,_}|Groups]) ->
[GroupId|get_group_id(Groups)];
get_group_id([]) -> [].
or use the lists:map/2 function:
get_group_id(Groups) ->
lists:map(fun ({group_info_db,GroupId,_,_,_,_,_}) -> GroupId end, Groups).
If instead of generating the members as one long string you instead generated a list of member name strings it would be much easier to work with. So that group info would become:
{group_info_id,"12err1","0","Atif_aslam2","+111@devlab","+111@devlab",
["+1@devlab","+1111@devlab","+919470220061@devlab","+111@devlab"]}
What type of info is it?