Help with making a list from table comparisons and output notice

Posted by Hoplo on Thu 30 Aug 2012 01:38 AM — 2 posts, 11,449 views.

#0
I have a "friends" function that checks the names obtained from who to a table with various character names and the clan they belong to. I works, but the final output format is not what I want, and I have not been able to figure out how to make it work.
the friend table looks like this:

friendstbl = {
Guardians = {"Balso", "Morti", "Salian"},
Regent = {"Hogt", "Filso", "Bese", "Moux"},
Fiendish = {"Tars", "Ruerk"},
}


and the function I currently use:
function dofriends()


 local fCount = 0
 if (whonames ~= nil and #whonames >= 1) then
  for i, c in pairs(friendtbl) do
   for n, m in pairs(c) do
    for a, b in ipairs(whonames) do
     if b == m then
       fCount = fCount + 1
       Note ("<"..i..">   "..b)
     end
    end
   end
  end
 end
end


This gives me an output of(those currently online):

<Guardians> Balso
<Guardians> Salian
<Regent> Filso
<Guardians> Morti

What I would like to have is something like:
3 <Guardians> Balso, Salian, Morti
1 <Regent> Filso

Any suggestions or help would be greatly appriciated. Thanks
Australia Forum Administrator #1

-- test data
whonames = { "Balso", "Salian", "Filso", "Morti" }



friendstbl = {
  Guardians = {"Balso", "Morti", "Salian"},
  Regent = {"Hogt", "Filso", "Bese", "Moux"},
  Fiendish = {"Tars", "Ruerk"},
}

if whonames == nil or #whonames < 1 then
  return
end -- if no-one online

-- convert people online into keyed table
local t = {}
for _, v in ipairs (whonames) do
  t [v] = true
end -- for

-- do each group of friends
for friendType, friendList in pairs (friendstbl) do
  local count = 0

  -- now check each friend to see if online
  for _, name in ipairs (friendList) do
     if t [name] then
       if count == 0 then
         Tell ("<" .. friendType .. "> ")
       else
         Tell (", ")
       end -- if
       count = count + 1
       Tell (name)
     end -- if that friend online
  end -- for
  if count > 0 then 
    Note ("")   -- newline
  end -- if
end -- for each type of friend



Output:


<Guardians> Balso, Morti, Salian
<Regent> Filso