How to create multi-dimensional table in C? (original) (raw)

lua-users home

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Aug 11, 2009 at 1:11 AM, Michael Newberrymnewberry@mirametrics.com wrote:

  // Now stack looks like  -2: RowTable,   -1: ColumnTable   // How do I do the equivalent of RT[ j ] = CT?

lua_rawseti(L, -2, j);

That'll set the item on the top of the stack (CT) to index j of the table at -2 (RT).

lua_newtable( L ); // push a Row Table (the main table) onto the stack for ( int j = 1; j <= nRows; j++ ) { lua_newtable( L ); // push a Column Table onto the stack for ( int i = 1; i <= nCols; i++ ) { lua_pushnumber( L, i ); lua_pushstring( L, sValue ); lua_settable( L, -3 ); }

lua_rawseti(L, -2, j); // RT[j] = CT }