[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Lua_next probles
- From: "BetGear - Glen" <glen@...>
- Date: Tue, 25 Sep 2007 16:11:37 +0100
Hi,
Just wanted to say thanks very much for the excellent advice given by both
Jerome, Luiz and Eva. My problem is now sorted :)
I've still got lots to learn, and you've helped me on my way.
Very much appreciated,
Glen.
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Jerome Vuarand
Sent: 25 September 2007 15:54
To: Lua list
Subject: RE: Lua_next probles
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.