Help file - table.insert

Posted by Fadedparadox on Wed 10 Jun 2009 09:38 PM — 3 posts, 14,028 views.

USA #0
I ran into a small mistake in the help file for the lua function table.insert that I didn't notice before. It says as follows:

"table.insert (t, pos, value)

Inserts the value at (optional) position 'pos', renumbering existing elements if necessary to make room. Thus the new element becomes the one with index 'pos'.

If called with 2 arguments, the value is inserted at n+1, that is, the end of the table."

It should have value and pos switched in either the prototype or the paragraphs. By the name I assume the prototype.
Amended on Wed 10 Jun 2009 09:44 PM by Fadedparadox
Australia Forum Administrator #1
The help file is in fact correct. I quote from the Lua 5.1 Reference Manual:

http://www.lua.org/manual/5.1/manual.html#5.5




table.insert (table, [pos,] value)

Inserts element value at position pos in table, shifting up other elements to open space, if necessary.

The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call table.insert(t,x) inserts x at the end of table t.




Note that the optional argument "pos" is in fact the second argument. This makes "value" the second or third argument, depending on whether or not pos is there.

I didn't put in the brackets, but the text of my help file states that the field pos is optional.
Amended on Thu 11 Jun 2009 01:18 AM by Nick Gammon
USA #2
I misunderstood the file, compounded by the fact that in a script I wrote, I switched the variables, seeming to back up what I read it as. Sorry about that!