|
In your code, you don't replace the table 'a' yet, because you only create a table in the stack that is in the function's stack and is discarded when the function return. Here, the table 'a' is a global var, and you can use lua_setglobal macro to replace the global var 'a' simply. eg: void array2table( lua_State *L, lua_Object lobj ) { if (lua_istable (L, lobj)) { printf ("element is table"); int arr[4] = { 5,100,-20,0 }; lua_newtable(L); // table for (int i=0; i<4; i++) { lua_pushinteger(L, i+1); // table,key lua_pushinteger(L, arr[i]); // table,key,value lua_settable(L,-3); // table } //////////////// insert the following line lua_setgloabl( L, "a" ); } } of course, the solution isn't perfect, the table 'a' must be a global and the "a" string is limited. the better solution is clear the table, then push the new value, as mention of the others. > Subject: Replace LUA table > Date: Tue, 28 Aug 2007 19:10:12 +0200 > From: Andreas.Volz@elektrobit.com > To: lua@bazar2.conectiva.com.br > > Hello, > > I try to replace a LUA table in C++. I do this in LUA: > > tc=TestClass:new () > a={10,11,12,13,14,15,16,17,18,19,20,21} > tc:testX (a) > print (a[1]) > print (a[2]) > print (a[3]) > > Printed from LUA: > 10 > 11 > 12 > > What does testX()? > > printf ("dump1:\n"); > stackDump (L); > > array2table( L, lobj ); > lua_remove(L, 2); > > printf ("dump2:\n"); > stackDump (L); > > ... and... > > void array2table( lua_State *L, lua_Object lobj ) > { > if (lua_istable (L, lobj)) > { > printf ("element is table"); > > int arr[4] = { 5,100,-20,0 }; > lua_newtable(L); // table > > for (int i=0; i<4; i++) > { > lua_pushinteger(L, i+1); // table,key > lua_pushinteger(L, arr[i]); // table,key,value > lua_settable(L,-3); // table > } > } > } > > This in printed in C++: > > 31018474 PID:1fe001a TID:53001e dump1: > 31018474 PID:1fe001a TID:53001e stack dump start > 31018474 PID:1fe001a TID:53001e userdata > 31018475 PID:1fe001a TID:53001e > 31018475 PID:1fe001a TID:53001e table > 31018476 PID:1fe001a TID:53001e table dump start > 31018476 PID:1fe001a TID:53001e 10 > 31018477 PID:1fe001a TID:53001e 11 > 31018477 PID:1fe001a TID:53001e 12 > 31018478 PID:1fe001a TID:53001e 13 > 31018479 PID:1fe001a TID:53001e 14 > 31018479 PID:1fe001a TID:53001e 15 > 31018480 PID:1fe001a TID:53001e 16 > 31018480 PID:1fe001a TID:53001e 17 > 31018481 PID:1fe001a TID:53001e 18 > 31018481 PID:1fe001a TID:53001e 19 > 31018482 PID:1fe001a TID:53001e 20 > 31018485 PID:1fe001a TID:53001e 21 > 31018485 PID:1fe001a TID:53001e table dump end > 31018486 PID:1fe001a TID:53001e > 31018487 PID:1fe001a TID:53001e > 31018488 PID:1fe001a TID:53001e stack dump end > 31018489 PID:1fe001a TID:53001e element is table > > 31018490 PID:1fe001a TID:53001e dump2: > 31018491 PID:1fe001a TID:53001e stack dump start > 31018492 PID:1fe001a TID:53001e userdata > 31018492 PID:1fe001a TID:53001e > 31018493 PID:1fe001a TID:53001e table > 31018493 PID:1fe001a TID:53001e table dump start > 31018495 PID:1fe001a TID:53001e 5 > 31018495 PID:1fe001a TID:53001e 100 > 31018496 PID:1fe001a TID:53001e -20 > 31018496 PID:1fe001a TID:53001e 0 > 31018497 PID:1fe001a TID:53001e table dump end > 31018497 PID:1fe001a TID:53001e > 31018498 PID:1fe001a TID:53001e > 31018498 PID:1fe001a TID:53001e stack dump end > > As you could see in 'dump2' I replaced the input table with my own table. Looks good so far. But why does LUA then print "10 11 12"? I would expect "5 100 -20". How could I access the table I replace in C++? > > If I don't "lua_remove(L, 2);" then the new table is after the input table. But is there any way to access this new table in LUA? > > regards > Andreas > > -- > Dipl.-Inf. (FH) Andreas Volz, ES1 > Elektrobit > Phone: +49 (9131) 7701 167, mailto:Andreas.Volz@elektrobit.com > Fax: +49 (9131) 7701 333, http://www.elektrobit.com > > Elektrobit Automotive GmbH, Am Wolfsmantel 46, 91058 Erlangen, Germany > Managing Director Otto Fößel > Register Court Fürth HRB 4886 > > > ---------------------------------------------------------------- > Please note: This e-mail may contain confidential information > intended solely for the addressee. If you have received this > e-mail in error, please do not disclose it to anyone, notify > the sender promptly, and delete the message from your system. > Thank you. > 不登录就能管理多个邮件帐户,试试 Windows Live Mail。 立即尝试! |