Traverse the Lua table structure in C

Source: Internet
Author: User
// Before performing the following steps, press the table to the top of the stack.
  Int Nindex = Lua_gettop (plua ); // Retrieve the table index value
Lua_pushnil (plua ); // Nil inbound stack as the initial key
  While ( 0   ! = Lua_next (plua, nindex ))
  {
//Currently, the top stack (-1) is value, and the position-2 is the corresponding key.
//Here we can determine what the key is and perform various processing on the value.
Lua_pop (plua,1);//Pop-up value to keep the key at the top of the stack
}  
  // Currently, the top of the stack is table.

The process of lua_next () is as follows:
1) A key pops up from the top of the stack.
2) Remove a key-value pair from the table at the specified position of the stack, first import the key into the stack, and then import the value into the stack.
3) If Step 1 is successful, a non-0 value is returned. Otherwise, 0 is returned and no value is input to the stack.

In step 2, retrieving the so-called "next key-value" from the table is relative to the key popped up in step 2. There is no data before the first key-value in the table. Therefore, press lua_pushnil () into a nil to act as the initial key.

Note that at the beginning, we first use lua_gettop () to obtain the positive index of the table in the stack (as mentioned earlier, we first import the table into the stack before performing the lua_next () process, therefore, the stack size is the positive index of the table. In the subsequent lua_next () process, there are constantly elements entering and exiting the stack. Therefore, it is convenient to use the positive index to locate the table.

When no key-Value Pair exists in the table, the last key is displayed in lua_next (). If no data exists, 0 is returned and the while loop ends. Therefore, after the lua_next () process ends, the table is located at the top of the stack.

Problem summary: I wrote a function using Lua to return a table. In C, I need to perform secondary processing on the elements in the returned table.
In C, we can use lua_gettable () or lua_rawget () to obtain the element values in the table. However, to use these two interfaces, you must know the key before it can give you the value. of course, it doesn't matter for the sequential subscript, but this time I used the associated array, the subscript is a non-Rule string. at this time, how to traverse the elements in the table is a problem.
Lua_next () is a suitable choice.

I introduced simple table traversal, but in fact I returned a two‑dimensional table. If you understand the processing process of lua_next (), the followingCode It seems that there is no problem.
T_idx = lua_gettop (L );
Lua_pushnil (L );

While (lua_next (L, t_idx ))
{
Printf ("=================================\ N ");
It_idx = lua_gettop (L );
Lua_pushnil (L );
While (lua_next (L, it_idx ))
{
Printf ("% s \ n", lua_tostring (L,-1 ));
Lua_pop (L, 1 );
}
Lua_pop (L, 1 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.