Lua and C + + fourth (get LUA table structure data)

Source: Internet
Author: User

This article was reproduced in: http://blog.csdn.net/musicvs/article/details/8445079

Stupid wood to contribute, what? Player? No, it's the heart.

Text:

1. What is a table

Table is the most powerful data type in Lua, and we can think of it as an array, but it's a bit different from the group, and it's recommended that you look at Lua's grammar tutorials, because I'm not familiar with table to explain to you.

(Narrator: What other tutorials do you write ...) )

2. Get the table variable

Now, let's add a table global variable to the Hellolua.lua file:

-- Hellolua.lua  file "beauty girl    " "Mutou "  the

We see that there is a hellotable variable, it is very similar to the array, and hashmap a bit similar, in short, it is very powerful.

(Narrator: I think the bright spot is that you have an IQ of 125?) I think it's possible to multiply by 2! )

Say, 125 times 2 equals what? ... 250 ..... o O!

The hellotable variable is obtained in the same way as before:

/** *      /"hellotable"

In this way, the hellotable variable is stored on top of the stack.

But we're not going to take the table variable, because C + + is a table type that doesn't recognize Lua, so we're going to get the exact value in the table, which is the value of name and IQ.

3. lua_gettable function

There is a function similar to Lua_getglobal, called Lua_gettable, as the name implies, it is used to obtain table-related data.

(Narrator: less nonsense ok = =)

The Lua_gettable function obtains a value from the top of the stack, then looks for the corresponding value in the table based on that value, and finally puts the found value on top of the stack.

The lua_pushstring () function can store strings in C + + in the LUA stack;

Then using Lua_gettable () to perform the previous steps, the second parameter of Lua_gettable is the index of the specified table variable in the stack.

(Narrator: Silly wood, I was you around the dizzy ...) )

To take care of the narrator, we draw a diagram to understand:

This is the initial state, there is nothing in the stack, so let's put the hellotable variable on top of the stack:

/**/"hellotable");        

And then it turns out that:

Next, we want to get the value of the table name, so the first thing to do is to put the "name" string into the stack:

1 /*  */  2     "name");  


And then it turns out:

(Narrator: Without this ah, you secretly added the index of the stack!) )

Note that I have added the index to the stack as we are about to use this time, we use a negative index (for friends who do not know the index of negative numbers please read the No. 03 chapter of the Tutorial Ha ~).

Because of the "name" in the stack, now the hellotable variable is not on the top of the stack.

Next, we call the most important step to get the value of name in table:

/*           look for a value for "name" from the Table object (the Table object is now in the stack indexed to-2, which is the current stack), and         after the corresponding value is obtained,     put the value back to the top of the stack *       / -2);  


At this point, the stack becomes this:

(Narrator: What happened?) Why does "Mutou" suddenly appear on the top of the stack?! For Mao! You painted it yourself! )

What did lua_gettable do to the bottom of the story?

First, let's explain the second parameter of lua_gettable, what does 2 mean, -2 is the index of the hellotable variable in the stack.

Lua then takes the value of the top of the stack (the top of the stack is "name") and then takes the value to find the corresponding value in the hellotable variable, and of course finds "Mutou".

Finally, Lua will put the found value into the stack, so "Mutou" to the top of the stack.

(Narrator: Your sister's paper ...) All right, I'll call it a curse.

Finally we only need to take out the data on the top of the stack, the complete code is as follows:

/*Initialize*/lua_state* PL =Lua_open ();        Luaopen_base (PL); /*Execute Script*/lual_dofile (PL,"Hellolua.lua"); /*Gets the table variable at the top of the stack*/Lua_getglobal (PL,"hellotable"); /*put the C + + string in the Lua stack, at which point the top of the stack becomes "name" and the Hellotable object becomes the bottom of the stack*/lua_pushstring (PL,"name"); /*look for a value for "name" from the Table object (the Table object is now in the stack indexed to-2, which is the current stack), and after the corresponding value is obtained, put the value back to the top of the stack*/lua_gettable (PL,-2); /*now the value of the table name corresponds to the top of the stack, directly out of the*/      Const Char* SName = lua_tostring (PL,-1); Cclog ("name =%s", sName);

Okay, here's the end of this chapter.

Lua and C + + fourth (get LUA table structure data)

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.