Talk:Table.size: Difference between revisions
No edit summary |
No edit summary |
||
Line 20: | Line 20: | ||
if you are using a loop to create your table, it would be a simple matter to add it after: | if you are using a loop to create your table, it would be a simple matter to add it after: | ||
table = {} | table = {} | ||
local count = 0 | local count = 0 | ||
for _,v in pairs(results) do | for _,v in pairs(results) do | ||
table[v] = someFunction(v) | |||
count = count + 1 | |||
end | end | ||
table.setn(table, count) | table.setn(table, count) | ||
[http://www.lua.org/pil/19.1.html Array Size] | [http://www.lua.org/pil/19.1.html Array Size] | ||
Revision as of 19:58, 13 July 2009
What about using the builtin lua function:
table.getn(table)
--Subenji99 06:54, 13 July 2009 (CEST)
table.getn was replaced by the #-operator, which returns the number of table elements with a numerical (!) index.
table.size counts all elements regardless of which type their index is.
Of course it's a bad idea to use this function for a table with only numerical indices.
NeonBlack 16:19, 13 July 2009 (CEST)
Indded, I actually just attempted it with non-numerical indices and found it didn't work. I stand corrected. :D
I shall leave my humiliation (lol) here so anyone else wondering what the point of this function would be can see that it is for non-numerical indices.
Oh, reading up, I'd like to point out that if you structure your table, planning for this, you can still use the # operator. you have to count the additions you make to your table then use table.setn like so:
table.setn(table, <number of values>)
if you are using a loop to create your table, it would be a simple matter to add it after:
table = {} local count = 0 for _,v in pairs(results) do table[v] = someFunction(v) count = count + 1 end table.setn(table, count)
In my case though, this function was still required as the table I was checking against was a returned table from an exported function of mapmanager.
--Subenji99 20:43, 13 July 2009 (CEST)