DVK
DVK

Reputation: 129481

Does "n" key have some special meaning in Lua tables?

http://lua-users.org/wiki/CppLuaDataPassing has this code to create a Lua table from C++:

   // set first element "1" to value 45
   lua_pushnumber( state, 1 );
   lua_pushnumber( state, 45 );
   lua_rawset( state, -3 );

   // set the number of elements (index to the last array element)
   lua_pushliteral( state, "n" );
   lua_pushnumber( state, 1 );
   lua_rawset( state, -3 );

It seems that the last block implies that Lua tables have some special-meaning key of "n" which stores the index to the last array element, based on that example.

But I couldn't find any reference to that in Lua Manual.

Upvotes: 2

Views: 224

Answers (2)

Mike Corcoran
Mike Corcoran

Reputation: 14565

It used to be a convention to hold the size of the table. I believe in lua 5.1 they deprecated that as a practice in favor of the # operator, as there were times when it would seemingly magically conflict with data people were stuffing in their tables.

Upvotes: 2

user1632861
user1632861

Reputation:

Take a look at this: http://www.lua.org/pil/19.1.html

n represents the length of an array. It is most commonly used with getn() function, which simply returns the amount of elements in the table.

Upvotes: 0

Related Questions