Tables - WoWInterface
07-11-10, 05:32 PM   #1
wellbeing
A Cliff Giant
Join Date: Oct 2009
Posts: 71
Tables

Consider:

day = the day
ate = number of times i ate in day
food = ate food
candy = ate candy
drank = number of times i drank in day
peed = number of times i peed in day
gallonsPeed = number of gallons i peed that time
poopt = number of times i poopt in a day
regnum = regnum+1
all the values are being sent to the table as integers on a constant stream, creating a large table.

if i need to be able to keep track of this data and sort.. what is the best way to do it?

Register={
["day"]=day,
["ate"]=ate,
["fd"]=food,
["cd"]=candy,
["dr"]=drank,
["pd"]=peed,
["gl"]=gallonSpeed -- for this exercise we will assume i only pee in round gallons
["pt"]=poopt
["rn"]=regnum
}

--if i want to be able to call up and print the following:

--on the [candy] days that you ate candy, you pooped [poopt] times total and peed [peed] times resulting in [gallonsPeed] gallons peed total.

for regnum=1, regnum do

print("on the ", Register.cd, " days that you ate candy, you pooped ", Register.pt, " times total and peed", Register.pd, " times resulting in ", Register.gp, " gallons peed total.")

--obviously, this only prints the current register and not an accrued amount.

my table has 17 variables for each entry and i need to be able to call totals from some variables based on whether other variables are (>0) or (==0).

is this even the right/most efficient method of doing this?
if so, how do i add the values up for printing?

thanks for suffering my awful example.

07-11-10, 07:25 PM   #2
Slakah
A Molten Giant

Join Date: Aug 2007
Posts: 863
lua Code:
`local registers = {    {value1 = 1, value2 = 3},    {value1 = 1, value2 = 100},    {value1 = 1, value2 = 365}} local total_value1, total_value2 = 0, 0for regnum, register in pairs(registers) do    total_value1 = total_value1 + register.value1    total_value2 = total_value2 + register.value2end print("The total of value1 is", total_value1, "The total of value2 is", total_value2)`
Would output "The total of value1 is 3 The total of value2 is 468"

As a quick example.

Last edited by Slakah : 07-11-10 at 07:31 PM.

07-13-10, 10:55 AM   #3
eGoh
A Defias Bandit
Join Date: Mar 2010
Posts: 3
Testing out the code formatting, but might as well post something useful while I'm at it!

Code:
`local register = {}`
Code:
```function AddData(day, ate, food, candy, drank, peed, gallonsPeed, poopt, regnum)
register[day] = {
["ate"] = ate,
["food"] = food,
["candy"] = candy,
["drank"] = drank,
["peed"] = peed,
["gallonsPeed"] = gallonsPeed,
["poopt"] = poopt,
["regnum"] = regnum
}
candyindex[day] = true
end```
This creates a table using "day" as the primary key and it has to be unique for every entry. This also means that searches based on the "day" will be very fast. i.e.

Code:
```function GetDayData(day)
local value = register[day]
return value.peed, value.gallonsPeed, value.poopt
end```
However, aggregate functions like the one you want will get slower as you add more entries to the table. Simply, because you have to loop through every value. This is no problem if you only want to store a few hundred entries.

Code:
```function GetCandyData()
local poopt=0
local peed=0
local gallonsPeed=0

for i,v in pairs(register) do
if candy then
poopt =  poopt + v.poopt
peed =  peed + v.peed
gallonsPeed =  gallonsPeed + v.gallonsPeed
end
end

return poopt, peed, gallonsPeed
end```
If you are going to store lots and lots of entries, AND you know that you are going to do this sort of search often, AND you know that you don't eat candy very often, you can create an index, and populate it every time you add to the table.

Code:
```local register = {}
local candyindex = {}

function AddData(day, ate, food, candy, drank, peed, gallonsPeed, poopt, regnum)
register[day] = {
["ate"] = ate,
["food"] = food,
["candy"] = candy,
["drank"] = drank,
["peed"] = peed,
["gallonsPeed"] = gallonsPeed,
["poopt"] = poopt,
["regnum"] = regnum
}
candyindex[day] = true
end```
So when you rewrite, the candy function, it will be more efficient, and only slow down when you eat too much candy, as opposed to the number of days you record.

Code:
```function GetCandyData()
local poopt=0
local peed=0
local gallonsPeed=0

for i,v in pairs(candyindex) do
local values = register[i];
poopt =  poopt + values.poopt
peed =  peed + values.peed
gallonsPeed =  gallonsPeed + values.gallonsPeed
end

return poopt, peed, gallonsPeed
end```
Of course, if you eat candy every day, then this code will most likely end up being slower than without the index!

P.S. Okay! Okay! I give up! How do you switch on the fancy schmancy syntax hilighting?

Last edited by eGoh : 07-13-10 at 01:59 PM. Reason: careless coding

07-14-10, 07:56 PM   #4
wellbeing
A Cliff Giant
Join Date: Oct 2009
Posts: 71
I'm still struggling with this:

if i declare my topmost table outside function:
Code:
`local skillzmain = {}`
then, inside the function, after the skillzmain table has been populated:

Code:
```if (skillzmain.sp == 2) then

local dudename = skillzmain.sn
print ("*"..dudename.."*")
if skillzmain.dudeTable == nil then
skillzmain.dudeTable = {}

print("created skillzmain.dudeTable.")
end
if skillzmain.dudeTable.dudename == nil then
skillzmain.dudeTable.dudename = {
["ts"]=timestamp,
["spn"]=sName,
["ha"]=tonumber(Amt),
["oh"]=tonumber(Amt2),
["cr"]=wasC,
["dn"]=destName,
["rn"]=duderegnum
}
print("created skillzmain.dudeTable."..dudename)
end
end```
it doesnt seem to be saving the tables.

if i call to the function with proper variables.. the tables get created, as will be shown by the prints inside each.

if i call to it again, it creates them all again, as shown by the prints within them again.

do i need to somehow declare every subtable globally? am i not properly doing this? maybe theres a problem with how im using the variable to name the table..

lost.

07-14-10, 08:06 PM   #5
Slakah
A Molten Giant

Join Date: Aug 2007
Posts: 863
Surely it should be "~=" not "==" if you want make sure the table is there.

Also I would just like to point out skillzmain.dudeTable.dudename, has the table structure
lua Code:
`skillzmain = {    ["dudeTable"] = {        ["dudename"] = {}    }}`
Whereas you want dudename to be variable depending on the value of dudename so instead you should use skillzmain.dudeTable[dudename].

which is equivalent to
lua Code:
`skillzmain = {    ["dudeTable"] = {        [dudename] = {}    }}`
notice how dudename is now a variable instead of a string.

07-14-10, 08:13 PM   #6
wellbeing
A Cliff Giant
Join Date: Oct 2009
Posts: 71
 Originally Posted by Slakah Surely it should be "~=" not "==" if you want make sure the table is there.
well, im only wanting to go through that part of the function if the table isnt there.. to create it.

ultimately, ill have an addData() function for adding the data.. but as the example currently is, assume i'm going to populate the list normally within that function if the table ~= nil.

with that in mind, it makes sense the way i have it, right? i just want to make sure im thinking about it the right way..

07-16-10, 07:37 AM   #7
Slakah
A Molten Giant

Join Date: Aug 2007
Posts: 863
 Originally Posted by wellbeing well, im only wanting to go through that part of the function if the table isnt there.. to create it. ultimately, ill have an addData() function for adding the data.. but as the example currently is, assume i'm going to populate the list normally within that function if the table ~= nil. with that in mind, it makes sense the way i have it, right? i just want to make sure im thinking about it the right way..
Yeah your right, I need to stop browsing the forums at ridiculous times :P, could you link all your code, I have a feeling your replacing the skillzmain table somewhere.

07-16-10, 04:00 PM   #8
wellbeing
A Cliff Giant
Join Date: Oct 2009
Posts: 71
 Originally Posted by Slakah Yeah your right, I need to stop browsing the forums at ridiculous times :P, could you link all your code, I have a feeling your replacing the skillzmain table somewhere.
i decided to go a different route altogether.

 WoWInterface » Tables

 Thread Tools Display Modes Linear Mode

 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