lua-users home
lua-l archive

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


Hi,

Iam trying to access the lua table values in c++,Iam not able to print the values.
here is the code of what iam doing.

-- A Table to be called from c++
-- imageTable.lua
imag = {"bg1", "bg2", "bg3", "bg4","bg5"}
print("done")


//   sample code

// luatable.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

extern "C" {
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
}

// the Lua interpreter
lua_State* L;

//global variable
size_t* len;


// A function to access a lua table in c++
int access_luaTable(lua_State* L)
{
	// temp variable
	const char* temp;

	//get the imag lua table from imageTable.lua
	lua_getglobal( L, "imag" );
	if( lua_istable( L, -1 ) )
	 {
		switch ( lua_type( L, lua_gettop( L ) ) )
		{
case LUA_TTABLE: std::cout << "script returned a table" << std::endl; break; default: std::cout << "script returned unknown param" << std::endl; break;
        }

		//int idx=lua_gettop( L );
		//lua_gettable(L, idx);
		lua_gettable( L, -1 );
		//lua_pushvalue(L,2);
		lua_rawget(L,-1);

		//lua_pushnil(L);  /* first key */
		while (lua_next(L, -1) != 0) {
        // `key' is at index -2 and `value' at index -1
printf("%s - %s\n",lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1)));
		 lua_pop(L, 1);  /* removes `value'; keeps `key' for next iteration */
		}
		return 0;
	 }
	if( lua_isnil( L, -1 ) )
	 {
		 return 0;
	 }


	return 0;
}


int main ( int argc, char *argv[] )
{
	/* initialize Lua */
	L = lua_open();

	/* load Lua base libraries */
	luaL_openlibs( L );

	/* load the script */
	luaL_dofile( L, "imageTable.lua" );

	/* call the a function */
	access_luaTable( L );

	/* cleanup Lua */
	lua_close( L );

	/* pause */
	printf( "Press enter to exit?" );
	getchar();

	return 0;
}

can somebody tell me what i should be doing to print values..

regards
Gokul

_________________________________________________________________
Shah Rukh fan? Know all about the Baadshah of Bollywood. On MSN Search http://server1.msn.co.in/profile/shahrukh.asp