Thread Tools Display Modes
08-04-07, 10:39 PM   #1
yssaril
A Warpwood Thunder Caller
 
yssaril's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 96
tables

am working on a database mod and instead of assigning things to tables like so:
Code:
table1[1] = "something1"
table1[2] = "something2"
table1[3] = "something3"
table1[4] = "something4"
i want to assign it like this:
Code:
table1["string1"] = "something1"
table1["string2"] = "something2"
table1["string3"] = "something3"
table1["string4"] = "something4"
now the creation of the table is the easy part but what i was wondering is there anyway for me to get back a list of the strings that where used without keeping track of the list myself?

say with a function like this:

Code:
returntableplace(table1)
this would then return {"string1","string2","string3","string4"}
  Reply With Quote
08-05-07, 06:36 AM   #2
Layrajha
A Frostmaul Preserver
 
Layrajha's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 275
Code:
for key, value in pairs(table) do
  DEFAULT_CHAT_FRAME:AddMessage("The key " .. key .. " is associated with the value " .. value .. ".")
  -- I am too lazy to check if the key and the value's type are ok to concatenate, but you get the idea
end
Edit:
The order in which the elements are returned is not specified. It can be different from the order in which the elements have been inserted, or different from the "<" order. If I remember well, there's a tutorial on wowace on how to add a sort function to your "pairs". If you need it, just google something like "site:wowace.com sortedpairs" and it should get you to the page you need.

Edit bis:
Nvm, it was on wowwiki: http://www.wowwiki.com/HOWTO:_Do_Tricks_With_Tables
It's also on wowace but in a more overall advanced tutorial: http://www.wowace.com/wiki/Coding_Ti..._keys_in_order

Last edited by Layrajha : 08-05-07 at 06:42 AM.
  Reply With Quote
08-05-07, 01:27 PM   #3
yssaril
A Warpwood Thunder Caller
 
yssaril's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 96
sweet that worked perfectly
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » tables


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