• Subject: RE: Lua_next probles
• From: "Jerome Vuarand" <jerome.vuarand@...>
• Date: Tue, 25 Sep 2007 10:53:47 -0400

```BetGear - Glen wrote:
> Ok, I'm going to post the parsing code, so you can pick it to bits :)
>
> void PrintTable(lua_State *L)
> {
> 	CString op;
> 	int		bah;
>
> 	lua_pushnil(L);
>
> 	while(lua_next(L, -2) != 0)
> 	{
> 		if(lua_isstring(L, -1))
> 		{
> 			if(lua_isstring(L, -2))
> 			{
> 				op.Format(_T("%s = %s\n"),
lua_tostring(L, -2), lua_tostring(L,
> 			-1)); }
> 			else
> 			{
> 				op.Format(_T("%d = %d\n"),
lua_tonumber(L, -2), lua_tostring(L,
> 			-1)); }
> 		}
> 		else if(lua_isnumber(L, -1))
> 		{
> 			if(lua_isstring(L, -2))
> 				op.Format(_T("%s = %d\n"),
lua_tostring(L, -2), lua_tonumber(L,
> 			-1)); else
> 				op.Format(_T("%d = %d\n"),
lua_tonumber(L, -2), lua_tonumber(L,
> 		-1)); }
> 		else if(lua_istable(L, -1))
> 			PrintTable(L);
>
> 		lua_pop(L, 1);
> 	}
> }

lua_isstring(L, -2) returns true for number keys (because Lua can
convert between string and numbers in all cases). Then lua_tostring(L,
-2) cast that stack slot to a string, which confuses lua_next. You
should use lua_type(L, -2)==LUA_TSTRING instead. lua_isnumber will also
return true for strings containing a number, same solution applies.

```