Sercankd
Sercankd

Reputation: 9

How to collect multiple data in a table then output like a concatenated string it in LUA

I am totally new to LUA scripting and trying to create a table with a data received one by one then use it in same function to output collected data.

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    battlegrounds = {};
    if string.find(arg1,"BGNUMBER") then
      number = string.match(arg1,"%d+");
      battlegrounds[number] = "";
      battlegrounds[number] = battlegrounds[number]..","..arg2;
      for k,v in pairs(battlegrounds) do
      DEFAULT_CHAT_FRAME:AddMessage ("\124c0000FFFF[BG Number:"..k.."]"..v.."");
      end
    end
  end
end

I tried to add data end of other data by using

battlegrounds[number] = battlegrounds[number]..","..arg2;

but it outputs only one data received, example first player name is Scarill and others are John, Max, it gets data from Scarill,John,Max but outputs like

[BG Number 43], Scarill

I want it to output like

[BG Number 43] Scarill, Max
[BG Number 54] John

Upvotes: 0

Views: 121

Answers (2)

Oliver
Oliver

Reputation: 29493

You need to extend the string every time the event handler is called:

local battlegrounds = {}

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    if string.find(arg1,"BGNUMBER") then
      number = string.match(arg1,"%d+")
      if battlegrounds[number] == nil then
          battlegrounds[number] = arg2
      else
          battlegrounds[number] = battlegrounds[number] .. "," .. arg2
      end
      for k,v in ...

But if PremadeFrame_OnEvent() can be called multiple times with same arg1 and arg2, but you duplicates in string, then your need your battlegrounds[number] to be a table and only add arg2 if not already in that table. Example (not tested, may be syntax errors):

local battlegrounds = {}

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    if string.find(arg1,"BGNUMBER") then
      local number = string.match(arg1,"%d+")
      if battlegrounds[number] == nil then
          battlegrounds[number] = {[arg2] = 1} -- use hash map, will be easiest to search for arg2 later
      else
          -- only add arg2 if not already in there:
          local bgn = battlegrounds[number] 
          if bgn[arg2] == nil then
              table.insert(bgn, arg2)
          end
      end

      for k,v in pairs(battlegrounds) do
          local msg = "\124c0000FFFF[BG Number:" .. k .. "]" .. table.concat(v, ',')
          DEFAULT_CHAT_FRAME:AddMessage(msg)
      end
    end
  end
end

Stylistic note: try to use local always except if need global

Upvotes: 0

pabdulin
pabdulin

Reputation: 35229

I guess that PremadeFrame_OnEvent is a event callback function. On each call new battlegrounds table is created inside so it's natural that it have only one item in it. It you want to capture more than current item, you need to make battlegrounds external to PremadeFrame_OnEvent like this:

battlegrounds = {}

function PremadeFrame_OnEvent(self, event, arg1, arg2)
  --start chat_msg_raid_leader
  if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" ) then
    if string.find(arg1,"BGNUMBER") then
      number = string.match(arg1,"%d+")
      battlegrounds[number] = arg2
      for k,v in pairs(battlegrounds) do
        DEFAULT_CHAT_FRAME:AddMessage ("\124c0000FFFF[BG Number:"..k.."]"..v.."")
      end
    end
  end
end

so it is not recreated each time in event handler.

Upvotes: 1

Related Questions