2016-12-29 2 views
3

안녕을 얻을 수있는 테이블은 다음과 같습니다액세스 중첩 테이블은 내가 C. 에 루아에서 보낸 중첩 된 테이블에 액세스하려고 값

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과 같습니다 난 아무것도없는

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++; 
} 

암, 값이 내가 돌아 0x0를 얻을 수 있기 때문이다.

+1

'lua_tonumber은 (L, -1)'잘못이지 수 (사람들은 arg' 테이블'에서 키 - 값 쌍의 값입니다) –

+1

' lua_pop (L, -1)'잘못된 것입니다. 슬롯 위치가 아니라 슬롯 수가 필요합니다. – lhf

+1

답 해 주셔서 감사합니다. 여기에서 답을 얻었습니다. http://stackoverflow.com/questions/27037854/lua-c-read-nested-tables – SanR

답변

1

이 기능을 필요에 맞게 변경하고 스택 맨 위에있는 표를 사용하여이 기능을 호출하십시오. 당신이 인덱스`-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 그런 다음 대답을 수락하십시오. –