Thread Tools Display Modes
04-10-10, 09:15 AM   #1
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Arrays and table.insert with named keys

Hello

I'm making my own dps addon, just for fun.

I'm trying to add all combatants to an array like this:

lua Code:
  1. players = {
  2.     ["Name1"] = {
  3.         ["class"] = "WARRIOR",
  4.         ["values"] = { 0,4,1 }
  5.     },
  6.     ["Name2"] = {
  7.         ["class"] = "ROGUE",
  8.         ["values"] = { 0,4,1 }
  9.     }
  10. }

The problem is, that table.insert does not let me insert named keys.... it just uses numbers.... so when I do table.insert(players,somedata), the result is:

lua Code:
  1. players = {
  2.     ["Name1"] = {
  3.         ["class"] = "WARRIOR",
  4.         ["values"] = { 0,4,1 }
  5.     },
  6.     ["Name2"] = {
  7.         ["class"] = "ROGUE",
  8.         ["values"] = { 0,4,1 }
  9.     },
  10.     [1] = somedata...
  11. }

I would like something like table.insert(players,mykeyname,somedata), but this obviously does not work ... :<
  Reply With Quote
04-10-10, 09:44 AM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
lua Code:
  1. players[mykeyname] = somedata
  Reply With Quote
04-10-10, 10:09 AM   #3
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
A tip mate, try to avoid table.insert AS MUCH AS POSSIBLE. It's really inefficiënt.

If it's a number indexed table this is always faster then table.insert:
Code:
local function tinsert(tab, val)
    tab[#tab+1] = val
end
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
04-10-10, 10:31 AM   #4
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by nightcracker View Post
A tip mate, try to avoid table.insert AS MUCH AS POSSIBLE. It's really inefficiënt.
Meh, it's still pretty insignificant difference, and by converting it to a function as you do, pretty much nulls any performance gain.

http://www.pastey.net/135124-3xsy

results:
Code:
table.insert took 	0.02700 sec
local table.insert took 	0.02300 sec
new tableinsert func took 	0.02500 sec
arr[#arr + 1] = took 	0.00800 sec

Last edited by Slakah : 04-10-10 at 10:37 AM.
  Reply With Quote
04-10-10, 11:32 AM   #5
Beoko
Guest
Posts: n/a
Another important thing to note is that table.insert and table.remove are required if you want to properly resize the table.

http://www.lua.org/pil/19.2.html

An example:
Code:
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> T = { 1, 2, 3, 4 }
> T[2] = nil
> for Index = 1, #T do print(Index, T[Index]) end
1       1
2       nil
3       3
4       4
> for Index, Value in ipairs(T) do print(Index, Value) end
1       1
> for Key, Value in pairs(T) do print(Key, Value) end
1       1
3       3
4       4
> T = { 1, 2, 3, 4 }
> table.remove(T, 2)
> for Index = 1, #T do print(Index, T[Index]) end
1       1
2       3
3       4
> for Index, Value in ipairs(T) do print(Index, Value) end
1       1
2       3
3       4
> for Key, Value in pairs(T) do print(Key, Value) end
1       1
2       3
3       4
If proper table resizing is not a concern (it's not for most projects), then inserting or removing entries manually is the most efficient solution - as pointed out in Slakah's example.
  Reply With Quote
04-10-10, 12:45 PM   #6
Shadowed
...
Premium Member
Featured
Join Date: Feb 2006
Posts: 387
Originally Posted by Slakah View Post
Meh, it's still pretty insignificant difference, and by converting it to a function as you do, pretty much nulls any performance gain.

http://www.pastey.net/135124-3xsy

results:
Code:
table.insert took 	0.02700 sec
local table.insert took 	0.02300 sec
new tableinsert func took 	0.02500 sec
arr[#arr + 1] = took 	0.00800 sec
And odds are, you aren't inserting entries into a table often enough that it's noticeable.
  Reply With Quote
04-10-10, 04:29 PM   #7
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Shadowed View Post
And odds are, you aren't inserting entries into a table often enough that it's noticeable.
Not with my library keeping track of GUID's to transform GUID's to UnitID's
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
04-10-10, 04:47 PM   #8
Shadowed
...
Premium Member
Featured
Join Date: Feb 2006
Posts: 387
Originally Posted by nightcracker View Post
Not with my library keeping track of GUID's to transform GUID's to UnitID's
No, it's still not worth it. Slakah's test was 100,000 iterations, the difference between a local table.insert and a arr[#arr + 1] is 0.015 seconds. You "save" 0.00000015 seconds per call, nobody is going to notice that kind of optimization and it's pointless to do. Even if you're (for some reason) inserting 100,000 rows into a table, the extra 0.015 seconds really will not be noticed.
  Reply With Quote
04-10-10, 09:21 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Here's the thread/post I was looking for... http://forums.wowace.com/showthread.php?t=10301

(This is the book: http://www.amazon.com/Preventative-P.../dp/1584502576 )



Originally Posted by Donald Knuth, "Structured Programming with go to Statements"
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
Discussion: http://stackoverflow.com/questions/2...ot-of-all-evil
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
04-10-10, 10:25 PM   #10
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Seerah View Post
Here's the thread/post I was looking for... http://forums.wowace.com/showthread.php?t=10301
Really? It doesn't seem to have much to do with whats going on here (unless of course it's going completely over my head).

I think rule #1 of http://www.lua.org/gems/sample.pdf suits more.
  Reply With Quote
04-10-10, 11:20 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Sorry, I just meant for nightcracker to read all of it. Premature Optimization is mentioned in post #18 of that thread (I forgot to mention that).
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
04-11-10, 06:23 AM   #12
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Seerah View Post
Sorry, I just meant for nightcracker to read all of it. Premature Optimization is mentioned in post #18 of that thread (I forgot to mention that).
Sorry, I just remembered this post(took me a while to find it):
http://www.wowwiki.com/API_tinsert

I only just realized(after reading it again) that they use 5e6, which is 5000000.
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Arrays and table.insert with named keys


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