title: Iterating over table of tables with the Lua C API
body: <p>I'm trying to iterate over a table of tables in Lua and output:</p>
  
  <ul>
  <li>The key of each table.</li>
  <li>The key / value pair of each entry in each table.</li>
  </ul>
  
  <p>Here is the code:</p>
  
  <pre><code>void print_table(lua_State *L)
  {
      lua_pushnil(L);
      while(lua_next(L, -2) != 0) {
      const char *key = lua_tostring(L, -2);
              if(lua_isstring(L, -1))
                  printf("%s = %s", key, lua_tostring(L, -1));
              else if(lua_isnumber(L, -1))
                  printf("%s = %d", key, lua_tonumber(L, -1));
              else if(lua_istable(L, -1)) {
                  printf("%s", key);
                  PrintTable(L);
              }
              lua_pop(L, 1);
          }
      }
  }
  </code></pre>
  
  <p>And here is an example of one of a table I'm trying to output:</p>
  
  <pre><code>s = {
          p = {
              n = "D",
              g = "1",
          },
          d = {
              l = "N",
              p = "N",
              u = "O",
              po = 100,
          },
          e = {
              {
                  n = "B",
                  l = "P",
                  p = "P",
                  u = "P",
                  po = "P",
                  pa = {
                      v = "4",
                      a = "U",
                      f = {
                          { name = "U", type = "U" },
                          { name = "A", type = "I" },
                          { name = "A", type = "I" },
                          { name = "P", type = "U" },
                          { name = "P", type = "U" },
                          { name = "P", type = "I" },
                          { name = "T", type = "U" },
                          { name = "D", type = "U" },
                          { name = "D", type = "I" },
                          { name = "S", type = "I" },
                          { name = "C", type = "U" },
                          { name = "G", type = "U" },
                          { name = "C", type = "F" },
                          { name = "C", type = "U" },
                      },
                  },
                  c = {
                      v = "1",
                      a = "",
                      f = {
                          { name = "B", type = "U" },
                          { name = "E", type = "F" },
                      },
                  },
              },
          },
      }
  </code></pre>
  
  <p>The function crashes in the line:</p>
  
  <pre><code>while(lua_next(L, -2) != 0)
  </code></pre>
  
  <p>due to an invalid index.  The line of script that causes the crash is:</p>
  
  <pre><code>{ name = "B", type = "U" },
  </code></pre>
  
  <p>I have to admit I'm not massively familiar with the stack in Lua, I've tried searching for similar answers and was unable to find any.  Anyone know what I'm doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p>Added working version in case anyone is interested:</p>
  
  <pre><code>void print_table(lua_State *L)
      {
          if ((lua_type(L, -2) == LUA_TSTRING))
              printf("%s", lua_tostring(L, -2));
  
          lua_pushnil(L);
          while(lua_next(L, -2) != 0) {
              if(lua_isstring(L, -1))
                  printf("%s = %s", lua_tostring(L, -2), lua_tostring(L, -1));
              else if(lua_isnumber(L, -1))
                  printf("%s = %d", lua_tostring(L, -2), lua_tonumber(L, -1));
              else if(lua_istable(L, -1)) {
                  print_table(L);
              }
              lua_pop(L, 1);
          }
      }
  </code></pre>