The first sentence states "Adding an element to an array is as simple as associating the value to the next integer key in sequence" followed by example tbl[#tbl] = "new element"

It should be tbl[#tbl + 1] = "new element"

Posted by Nosnam on Thu, 06 Nov 2008 23:27:18

The first sentence states "Adding an element to an array is as simple as associating the value to the next integer key in sequence" followed by example tbl[#tbl] = "new element"

It should be tbl[#tbl + 1] = "new element"

Posted by Fogger on Wed, 28 Jan 2009 03:09:41

I came here to mention this also, and was'nt sure I could just add the +1

thanks for pointing it out. :)

Posted by morlando on Thu, 29 Jan 2009 16:16:30

Thanks for the note. I'll add this to the errata.

It actually may have been on the errata at one point, but we had to restore the site from backups a while back (no) thanks to our previous host.

Posted by mshirey on Sat, 31 Jan 2009 13:50:02

Which is actually more efficient?

tbl[#tbl + 1] = "new element"

or

table.insert(tbl, "new element")

or is it pretty much a wash and just a matter of style?

-- Matthew

Posted by jnwhiteh on Sat, 31 Jan 2009 22:38:31

The fastest is the following:

local c = #tbltbl[c] = newValue;c = c + 1

But you should not be worry about things like that at this stage. Premature optimization is the root of all evil and it's much more important that your code works correctly before nit-picking =).

Posted by mshirey on Sat, 31 Jan 2009 23:58:19

No worries on that part. There's not much optimizing happening here yet. It was just a matter of curiousity more than anything. I couldn't agree more to your statement about premature optimization. Thank you for the information.