2016-12-29 4 views
3

こんにちはを取得するための表では、次のとおりです。アクセスネストした表は、私はC. へのLuaから送られた、ネストされたテーブルにアクセスしようとしていた値

arg = 
{ 
    MagicNumber = {MagicNumber, 0},  
    ProdNum = {ProdNum, 1}, 
    LetterR = {LetterR,  0xc}, 
    Revision = {Revision, 0xd}, 
    Space1 = {Space1,  0xe}, 
    MnfctrCode = {MnfctrCode, 0xf}, 
    Hyphen1 = {Hyphen1,  0x12}, 
    ZeroCode = {ZeroCode, 0x13},  
    Hyphen2 = {Hyphen2,  0x15}, 
    MnfctrMnth = {MnfctrMnth, 0x16}, 
    MnfctrYear = {MnfctrYear, 0x18}, 
    SerialNum = {SerialNum, 0x1a}, 
    Space2 = {Space2,  0x1e}, 
    ChkSum = {ChkSum,  0x1f}, 
} 

の表は、内部の両方の整数値であり、テーブルのキーは文字列です。私のコードスニペットはollowsの通りです:私は戻って取得値が0x0あるので

lua_pushnil(L); 

while(lua_next(L, -2) != 0) 
{ 
    field = lua_tostring(L, -2); 
    printf("\n %d field = %s", i, field); 
    wrData[i-1] = lua_tonumber(L,-1); 
    printf("\n data = 0x%x", wrData[i-1]); 
    lua_pop(L, -1); 
    i++; 
} 

は、私が何をしないのです。

+1

'lua_tonumber(L、-1)あなたは、インデックス' -1'でテーブルを持っているように '間違っている、いない番号(これらは' arg'テーブルから、キーと値のペアからの値です) –

+1

' lua_pop(L、-1) 'は間違っています:ポップするスロットの数で、スロットの数は必要ありません。 – lhf

+1

答えをありがとう、私はここから答えを得た:http://stackoverflow.com/questions/27037854/lua-c-read-nested-tables – SanR

答えて

1

この機能をニーズに合わせて変更し、スタックの上部にあるテーブルで呼び出すことができます。

void luaAccess(lua_State * L)// call this function with the table on the top of the stack 
    { 
     lua_pushnil(L); 
     while(lua_next(L, -2)) 
     { 
      switch(lua_type(L, -2)) 
      { 
       case LUA_TSTRING: 
        //deal with the outer table index here 
       break; 
       //... 
       // deal with aditional index types here 
      } 
      switch(lua_type(L, -1)) 
      { 
       case LUA_TTABLE: 
        lua_pushnil(L); 
        while (lua_next) 
        { 
         switch(lua_type(L, -2)) 
         { 
          case LUA_TNUMBER: 
           // deal with the inner table index here 
          break; 
          //... 
          // deal with aditional index types here 
         } 
         switch(lua_type(L, -1)) 
         { 
          case LUA_TSTRING: 
           //deal with strings 
          break; 
          case LUA_TNUMBER 
           //deal with numbers 
          break; 
         } 
         lua_pop(L, 1); 
        } 
       //... 
       // deal with aditional index types here 
       break 
      } 
     } 
     lua_pop(L, 1); 
    } 
+0

ありがとう!それはうまくいった! – SanR

+0

@SanjanaRakhechaそれでは答えを受け入れてください –

関連する問題