Lua C Api lua_gettable, lua_settable, Lua_next usage

Source: Internet
Author: User

Before the use of lua_gettable and lua_settable has not been clear today, the way to make notes.
1.lua_gettable


void Lua_gettable (lua_state *l, int index);

Pushes the t[k] value onto the stack, where the t valid index index points to the value, and the k top of the stack. This function pops up the key on the stack and places the result in the same position on the stack.

Here's an example:

// put a key on the top of the stack, this key is 1. If your key is a string, use lua_pushstring. 1);   // table starts at the top of the stack, which is 1, but the above statement presses a value and the stack top changes-2.    The function of the lua_gettable is to use the value of the top of the stack as the key to access the table on the 2 position. -2); 

Then the value of the 1th element in the table is placed on top of the stack, and you can use it if you want to use it.

Get TABLE element:

* Press the key of the element into the stack, using lua_gettable (Lua_state,index)

* For the string index , you can use Lua_getfield(Lua_state,index,key) to get directly,

such as: Lua_getfield (Stack,-1, "loaded"), equivalent to lua_pushstring (L, "Loaded") lua_gettable (l,-2);

What it says is a way to access an element in a table, so how do you traverse all the elements in the table?

1) If table is a table with continuous shaping as key, you can use the following method:

int size = Lua_objlen (l,-1); // related to #table    for (int1; i <= size; i++)  {  Lua_pushnumber (L, i);   -2);   // at this point the value of Table[i] is  at the top of the stack 1); // move the value of the top of the stack out of the stack to ensure that the top of the stack is table for traversal.  };  

2) If the key in the table is any value? You can use the following method:

Lua_pushnill (L);    while (Lua_next (L,-2))  {  // This value is at-1 (top of the stack), key is at-2.   1); // move the value of the top of the stack out of the stack so that the key  becomes the top of the stack to continue traversal }  

Here are the highlights of lua_next.

It does this by taking the top element of the stack as key, first determining the value of the previous key (this value is placed at the top of the stack, and if it is nil, it means that the value of the first element in the table is currently fetched), and then the current key and value are obtained.

At this point the stack top out of the stack, the new key into the stack, and then the value into the stack. So the top of the stack is the value of the first traversed element in the table. After using this value, we want to put this value out of the stack, so that the new key on the top of the stack to continue the traversal. When the next key value is not calculated based on the previous key value (it is not important that the key is at this time, as long as it is not nil, as nil returns the first element of table), Lua_next returns 0, ending the loop.

2.lua_settable
void Lua_settable (lua_state *l, int index);

As an equivalent t[k] = v operation, here t is a value at a given valid index, which is the value at the top of the stack, and the value index v k below the top of the stack.

This function will eject both the key and the value from the stack.

In fact, the meaning of this explanation is that lua_settable will be the top of the stack as value, the next top of the stack as a key set to index point to the table, and finally the two pop-up pop-up stack, this time settable complete.

3.lua_next
int Lua_next (lua_state *l, int index);

POPs a key (key) from the stack, and then presses the Key-value (healthy value) pair into the stack (the next (next) pair after the specified key) in the table specified by the index. If there are no more elements in the table, then lua_next 0 is returned (nothing is pressed into the stack).

The typical traversal method is this:

     /*table placed at index ' t '*/Lua_pushnil (L); /*First Key*/      while(Lua_next (L, t)! =0) {       /*use ' key ' (at index 2) and ' value ' (at index-1)*/printf ("%s-%s\n", Lua_typename (L, Lua_type (L,-2)), Lua_typename (L, Lua_type (L,-1))); /*remove ' value '; Keep ' key ' for next iteration*/Lua_pop (L,1); }

When traversing a table, do not call the key directly lua_tolstring unless you know that the key must be a string. The call lua_tolstring has the potential to change the value of the given index position, which affects the next call lua_next .

Finally we call Lua's loaded table with a C and set a key that conforms to a certain rule to set its value to nil, which is the column:

1Ua_getglobal (Stack," Package");/*L:package, get the package, set in the stack*/2Lua_getfield (Stack,-1,"Loaded");/*L:package loaded, get the table, at the top of the stack*/3Lua_pushnil (stack);/*L:package loaded Nil*/4  while(0! = Lua_next (Stack,-2) )/*l:package loaded, key, value, last stack top nil, eject nil, get the first key and value of the table, press into the stack*/5 {6     //Cclog ("%s-%s \ n", tolua_tostring (Stack,-2, ""), Lua_typename (Stack, lua_type (Stack,-1)));7STD::stringKey=tolua_tostring (Stack,-2,"");/*this time the stack top is the next, is key*/8STD::stringTablekey =key;/*Here's a piece of processing for key*/9     intFound = Tablekey.rfind (". Lua");Ten     if(FOUND!=STD::string:: NPOs) OneTablekey = Tablekey.substr (0, found); ATablekey=replaceall (Tablekey,".","/"); -Tablekey=replaceall (Tablekey,"\\","/"); -Tablekey.append (". Lua"); theFound =Filename.rfind (tablekey); -     if(0= = Found | | (FOUND!=STD::string:: NPOs && filename.at (found-1) =='/')) -     { -Lua_pushstring (Stack, key.c_str ());/*Package loaded, key, Value,newkey, press key, push into the top of the stack*/ +Lua_pushnil (stack);/*pakage,loaded (table) ( -5), Key ( -4), Value ( -3), Key ( -2), nil ( -1)*/ -         if(Lua_istable (Stack,-5))/*Reading the top fifth of the stack is not a table*/ +         { A             /*result the value corresponding to key is set to nil*/ atLua_settable (Stack,-5);/*pakage,loaded (table), Key,value, sets the top two elements of the stack as key and value to table, pops up the top two elements of the stack*/ -         } -     } -Lua_pop (Stack,1);/*pakage,loaded (table), key pops up value, leaving key as next next*/ - } -Lua_pop (Stack,2);/*Stack Balance*/

Lua C Api lua_gettable, lua_settable, Lua_next usage

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.