PDA

View Full Version : Simple table question


Torros
24-10-2006, 08:12 AM
Hi,
I'm taking my first steps in writing an add-on and still struggling to get a good grasp on this .lua language. With all the sites and references I am making nice progress but there is one thing that keeps bugging me.
I just cannot seem to get my head around this whole table stuff. What I'd like is to create a multi dimensional table, use a charactername for key and a bunch of other attributes linked to that.
So I made a table with a characters name as key and another table as it's data.

I use this code for that:
--New Entry
tblReserves={[Sender]={Class="ClassData",Contact=Sender,Alternate="No",Note=".",AdminNote=".",JoinTime="Now"}}


That seems to work just fine. But no matter what I try I cannot seem to add another character to the table! When I revert the table to using numerical keys I will most probably get it to work but I would really like to get it to work this way. (I will never deny claims that I'm stubborn ;) )

I've tried adding the extra record with the following code (and more variations)
--Add Entry
table.insert (tblReserves,2,{[Sender]={Class="ClassData",Contact=Sender,Alternate="No",Note=".",AdminNote=".",JoinTime="Now"}});
table.insert (tblReserves,Sender,{Class="ClassData",Contact=Sender,Alternate="No",Note=".",AdminNote=".",JoinTime="Now"})


If anybody would be so good to point out the error in my way I would be very grateful.

Thanks,

Torros.

aspinkorgall
24-10-2006, 10:32 PM
I think your mixing up the two different types of table you can have in LUA. That is a key/value table and an index/value table.

You seem to want a key/value table where the key to each entry in the table is the Sender variable (lets assume a string). Assuming this, you have created the table with no problems, but to add a new entry, you can simply access it directly using the key:

tblReserves[Sender] = {Class="ClassData",Contact=Sender,Alternate="No",Note=".",AdminNote=".",JoinTime="Now"}You can check if an entry exists by doing:

if tblReserves[Name] then DEFAULT_CHAT_FRAME:AddMessage("I exist!!") end
I hope I've understood your intentions correctly, and that this is of assistance. If not, please let me know =)

Torros
25-10-2006, 08:26 AM
You've answered my question completely :thumbsup:

I guess I was just too fixed on using the .insert statement that I never even tried tried this simple method. I already said I can be a tad stubborn, did I? :rolleyes:

Many thanks!!

aspinkorgall
27-10-2006, 01:26 AM
I'm glad its helped. Good luck with the rest of your addon :)