Thread Tools Display Modes
06-08-12, 04:35 PM   #1
rx12network
A Murloc Raider
 
rx12network's Avatar
Join Date: Jun 2012
Posts: 4
Help with Table

Heyho,

i just try to beginn with lua.. but got my first problem... i try to make a table with indexes. i think you will know what i mean when you read the code below..

my problem is that the table will fill up in the loop (its ok) but he write just one line and overwrite it again again and again bla...

maybe someone can tell me how to work with auto-adding-rows in the loop.

P.s. sry for my bad eng - im german

Code:

Code:
ChatFrame1:AddMessage("----------------"); -- make seperator


for i=1,GetNumRaidMembers() do
   local name, rang, grp, lvl, klasse, class, zone, online, tot = GetRaidRosterInfo(i); -- read raidinfos
   
   if type(charinfo) ~= "table" then --check tableexist
      
      -- MAKE LIST with standart values if not exist
      charinfo = {}
      
      charinfo.cname = 1;
      charinfo.crang = 1;
      charinfo.cvisit = 0;
      charinfo.czone = 1;
      
   else  --otherwise
      
      
      if charinfo.cvisit ~= nil then  --check if player meeted befor
     
         
         charinfo.cname = name;  --fill db with values
         charinfo.crang = rang;
         charinfo.cvisit = 1;
         charinfo.czone = zone;
         
         print(charinfo.czone, charinfo.cname, charinfo.cvisit) --check
         
         
         ChatFrame1:AddMessage("Noch keine Begegnungen mit ".. charinfo.cname ..""); --chatmessage no visits befor
         
         
         
      else
         ChatFrame1:AddMessage("B: " .. charinfo.cname .. "| " .. charinfo.cvisit) -if visited chatmessage how often
         
         
         
         
      end
   end   
   
   
   
end
thanks
  Reply With Quote
06-08-12, 05:18 PM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Your table will look like this:

Code:
charinfo = {
	["cname"] = name,
	["crang"] = rang,
	["cvisit"] = 1,
	["czone"] = zone,
}
And you just update those 4 values with every iteration over the table.

What you want, I think, is this:

Code:
charinfo = {
	["1"] = {
		["cname"] = name,
		["crang"] = rang,
		["cvisit"] = 1,
		["czone"] = zone,
	},
	["2"] = {
		["cname"] = name,
		["crang"] = rang,
		["cvisit"] = 1,
		["czone"] = zone,
	},...
}
I don't have access to WoW atm but try this:

Code:
...
	charinfo[i] = {
		["cname"] = name,
		["crang"] = rang,
		["cvisit"] = 1,
		["czone"] = zone,
	}
...
  Reply With Quote
06-08-12, 05:22 PM   #3
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
charinfo[i].cname = name
.. etc
  Reply With Quote
06-09-12, 01:35 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
I'm not in game ... an easier way is to store the return of the function directly into a table.

Lua Code:
  1. local charinfo = {} -- use local variables where possible. Dont use generic names like 'charinfo' when you create global variables.
  2. for i=1,GetNumRaidMembers() do
  3.     local newchar = {GetRaidRosterInfo(i)} -- read raidinfos as a table
  4.     if(newchar[1] ) then -- only continue if name has a value
  5.         local found = false
  6.         for key, value in ipairs(charinfo) do -- check your 'charinfo' if the name exists.
  7.             if(value[1] == newchar[1]) then
  8.                 print("Found")
  9.                 found = true
  10.                 break -- stop the for-loop
  11.             end
  12.         end
  13.         if(not found) then
  14.             print("New char")
  15.             charinfo[#charinfo + 1] = newchar
  16.         end
  17.     end
  18. end
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
06-09-12, 05:57 AM   #5
rx12network
A Murloc Raider
 
rx12network's Avatar
Join Date: Jun 2012
Posts: 4
heyho,

thanks for the good and fast replys

i tried it now so and it looks that it work just the part with

if name == value then (please look in the code)

dont work realy couse its first cvisit, number and after it is cname, name. tried to lookup the index with

if value[1] == name but dont work too

again big thanks for helping a newbie

Lua Code:
  1. ChatFrame1:AddMessage("----------------");
  2.  
  3.  
  4. for i=1,GetNumRaidMembers() do
  5.    local name, rang, grp, lvl, klasse, class, zone, online, tot = GetRaidRosterInfo(i);
  6.    
  7.    
  8.    if type(chari[i]) ~= "table" then
  9.      
  10.       -- MAKE LIST
  11.      
  12.       chari[i] = {
  13.          cname = 0, crang = 0, cvisit = 1, czone = 0
  14.       }
  15.       print("lol")
  16.      
  17.    else
  18.      
  19.       for key, value in pairs(chari[i]) do
  20.        
  21.          if name == value then
  22.              print(key, value)
  23.             print("Found")
  24.                ChatFrame1:AddMessage("B: " .. chari[i]["cname"] .. " | " .. chari[i]["cvisit"])
  25.                chari[i]["cvisit"] = chari[i]["cvisit"] +1
  26.                break
  27.                
  28.             end
  29.            
  30.             for key, value in pairs(chari[i]) do
  31.                print(key, value)
  32.                if value ~= name then
  33.                  
  34.                   print("Not found")
  35.                   chari[i] = {
  36.                      cname = name, crang = rang, cvisit = 1, czone = zone
  37.                   }
  38.                  
  39.                   break
  40.                end
  41.                
  42.                
  43.             end
  44.          end  
  45.          
  46.       end
  47.      
  48.    end

**EDIT

Is there no way to work with a database? its stupid to work with nonshowed tables.. i saw gathermate for ex. work with a downable database from wowhead.
Some way for better parsing and multicolumtables like sql would be nice for select statements.

Last edited by rx12network : 06-09-12 at 06:33 AM.
  Reply With Quote
06-09-12, 07:43 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by rx12network View Post
Is there no way to work with a database? its stupid to work with nonshowed tables.. i saw gathermate for ex. work with a downable database from wowhead.
Some way for better parsing and multicolumtables like sql would be nice for select statements.
See Things Addons/Macros Can't Do. In particular, addons cannot see, read from, or write to any files on the hard disk, so no, it is not possible to work with any kind of "real" database like SQLite.

Addons that use the term "database" refer to data stored in simple Lua tables, including the addon that provides GatherMate with data from Wowhead.

I don't know what you mean by "nonshowed tables", but Lua tables are extremely simple to work with, and there's not really any need for complex SQL statements. It would be easy (though tedious and pointless) to write a wrapper around basic table operations to support SQL statements, but it would be far more effort than just using the basic table operations, so I can't imagine anyone actually doing it.

Finally, if you describe what goal you actually want to accomplish, we can almost certainly be more helpful in showing you the easiest way to achieve that goal.

Anyway, here are some examples that may or may not be helpful; without knowing what you actually want to do, it's pretty hard to tell you the best way to do it.

Lua Code:
  1. -- Main storage table:
  2. local alldata = {}
  3.  
  4. -- Populate the table:
  5. local function UpdateData()
  6.     local numRaidMembers = GetNumRaidMembers()
  7.  
  8.     for i = 1, numRaidMembers do
  9.         -- Get info about this raid member:
  10.         local name, rank, subgroup, level, className, class, zone, online, isDead, role, isML, combatRole = GetRaidRosterInfo(i)
  11.  
  12.         local chardata
  13.  
  14.         -- Check if data already exists at this index:
  15.         if alldata[i] then
  16.             -- If so, clear it and reuse the table:
  17.             chardata = wipe(alldata[i])
  18.         else
  19.             -- Otherwise, create a new table:
  20.             chardata = {}
  21.         end
  22.  
  23.         -- Fill in the table:
  24.         chardata.index, chardata.name, chardata.rank, chardata.subgroup, chardata.level, chardata.className, chardata.class, chardata.zone, chardata.online, chardata.dead, chardata.role, chardata.masterLooter, chardata.combatRole = i, name, rank, subgroup, level, className, class, zone, online, isDead, role, isML, combatRole
  25.  
  26.         -- Create or update the data at this index in the main storage table:
  27.         alldata[i] = chardata
  28.     end
  29.  
  30.     if numRaidMembers < #alldata then
  31.         -- Remove data at unused indices:
  32.         for i = numRaidMembers + 1, #alldata do
  33.             -- It would be more efficient to recycle the tables, but
  34.             -- that is beyond the scope of this simple example, so here
  35.             -- we will just let the garbage collector handle it:
  36.             alldata[i] = nil
  37.         end
  38.     end
  39. end
  40.  
  41. -- Update the table each time the raid roster changes:
  42. local frame = CreateFrame("Frame")
  43. frame:RegisterEvent("RAID_ROSTER_UPDATE")
  44. frame:SetScript("OnEvent", UpdateData)

To get information about a character by their name:

Lua Code:
  1. -- Function to return information about the named character:
  2. local function GetCharacterByName(name)
  3.     -- Loop through the data at each index:
  4.     for i, chardata in ipairs(alldata) do
  5.         -- Check the data's name value against the search value:
  6.         if chardat.name == name then
  7.             -- If it matches, return the character's data table.
  8.             return chardata
  9.         end
  10.     end
  11. end
  12.  
  13. -- Get information about the character whose name is "Benny":
  14. local Benny = GetCharacterByName("Benny")
  15. if Benny then
  16.     print(string.format("%s is a level %d %s in group %d at index %d.", Benny.name, Benny.level, Benny.className, Benny.subgroup, Benny.index))
  17. else
  18.     print("Benny is not in the raid.")
  19. end

To get a count of all characters who are rogues:

Lua Code:
  1. -- Function to get a count of all characters with the specified class:
  2. local function GetNumCharactersByClass(class)
  3.     local count = 0
  4.     for i, chardata in ipairs(alldata) do
  5.         if chardata.class == class then
  6.             count = count + 1
  7.         end
  8.     end
  9.     return count
  10. end
  11.  
  12. -- Get a count of all rogues:
  13. print(string.format("There are %d rogues in the raid.", GetNumCharactersByClass("ROGUE")))

To get information about all the characters in group 3:

Lua Code:
  1. -- Function to get information about all characters in a group:
  2. local function GetCharactersByGroup(group)
  3.     local results = {}
  4.     for i, chardata in ipairs(alldata) do
  5.         if chardata.subgroup == group then
  6.             tinsert(results, chardata)
  7.         end
  8.     end
  9.     return results
  10. end
  11.  
  12. -- Get information about group 3:
  13. local group3 = GetCharactersByGroup(3)
  14. if #group3 == 0 then
  15.     print("Group 3 is empty!")
  16. else
  17.     local names
  18.     for i, chardata in ipairs(group3) do
  19.         if names then
  20.             if i == #group3 then
  21.                 -- Last name on the list:
  22.                 names = names .. ", and " .. chardata.name
  23.             else
  24.                 -- Name in the middle of the list:
  25.                 names = names .. ", " .. chardata.name
  26.             end
  27.         else
  28.             -- First name on the list:
  29.             names = chardata.name
  30.         end
  31.     end
  32.     print(string.format("There are %d characters in group 3: %s.", #group3, names))
  33. end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
06-09-12, 08:04 AM   #7
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Its difficult to help without knowing a little bit more about how you want to use the information.
Writing in your table using the current raidslot as index makes it difficult to keep the data valid since raidslots can change when someone is moved around,joins or leaves. And if you want to gather the informations over a longer period with different raids then the ids are meaningless.

If you want to store information about a char and store it in savedvariables perhaps you should use the name (and server) instead of the raidslot as index.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
06-09-12, 08:23 AM   #8
rx12network
A Murloc Raider
 
rx12network's Avatar
Join Date: Jun 2012
Posts: 4
at first big thanks at Phanx for the nice codesnippets ^_^

what i want to do:
i hear in teamspeak in random raids always "hey.. i think i saw you before blabla"

so i want to try to write a addon that catch informations from persons that are in the raid and save it with timestamp, class, lvl etc (much of them from raidrosterinfo).

So when raid is full the addon can check ahh u saw person xy 6 times before.. with a click in the gui he can lookup more information about the person.

i know it will cost much of time.. im reading atm the book "Beginning Lua with World of Warcraft".
And i downloadet me the LuaBrowser (addon for wow)... its helpfull to understand tables in lua ^^ i think i think to complicated from c# and sqllite hehe

------ EDIT ------
i think i finaly got it to work
here is the code

Lua Code:
  1. ChatFrame1:AddMessage("----------------"); -- Add Message to Chat
  2.  
  3.  
  4. for i=1,GetNumRaidMembers() do  --Get Raid Information
  5.    local name, rang, grp, lvl, klasse, class, zone, online, tot = GetRaidRosterInfo(i);
  6.    
  7.    
  8.    
  9.    if type(RaidChecker) ~= "table" then  --check if table exist
  10.      
  11.       -- MAKE LIST
  12.      
  13.       RaidChecker = {
  14.       }
  15.      
  16.      
  17.    else --otherwise
  18.      
  19.       if RaidChecker[name] == nil then  --if name not in table
  20.          
  21.          RaidChecker[name] = {  --put informations in table
  22.             cname = name,
  23.             cvisit = 1,
  24.             crang = rang,
  25.             clvl = lvl,
  26.             cklasse = klasse,
  27.             cclass = class,
  28.             zone = zone
  29.          }
  30.          
  31.          print("Neue Nutzer Angelegt!") --just debugging
  32.          break
  33.          
  34.       end
  35.      
  36.      
  37.      
  38.       for key, value in pairs(RaidChecker) do
  39.          
  40.          if RaidChecker[name]["cname"] == name then --if name exist
  41.            
  42.            
  43.             ChatFrame1:AddMessage("B: " .. RaidChecker[name]["cname"] .. " | " .. RaidChecker[name]["cvisit"])
  44.             RaidChecker[name]["cvisit"] = RaidChecker[name]["cvisit"] +1 --ad one visit
  45.             print("User found, adding a visit")
  46.             break
  47.            
  48.          end
  49.          
  50.       end
  51.      
  52.      
  53.      
  54.      
  55.      
  56.      
  57.    end  
  58.    
  59. end

Last edited by rx12network : 06-09-12 at 09:43 AM.
  Reply With Quote
06-09-12, 10:52 PM   #9
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Phanx View Post
It would be easy (though tedious and pointless) to write a wrapper around basic table operations to support SQL statements, but it would be far more effort than just using the basic table operations, so I can't imagine anyone actually doing it.
Actually, ckknight went a little insane and wrote the 9k-lines LibLinq. Though not actually SQL, it's damn close and I still have no idea why he did this.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
06-10-12, 04:13 PM   #10
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Humbedooh had started LibSQL-1.0 but he dropped it at the same time he dropped WoW
  Reply With Quote
06-10-12, 09:57 PM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Still, why bother with all that overhead? Not only will using Lua table operations directly always be more efficient than going through a wrapper, but understanding what you're actually doing will make you a better programmer.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
06-11-12, 12:08 AM   #12
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Phanx View Post
Still, why bother with all that overhead? Not only will using Lua table operations directly always be more efficient than going through a wrapper, but understanding what you're actually doing will make you a better programmer.
I totally agree - I was just pointing out that there ARE folks who are that insane.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with Table


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off