Lua Study Notes

Source: Internet
Author: User

 

I have plenty of time recently. I want to take a look at the Lua language and write down my notes so that I can learn more in the future.

For convenience, I did not compile Lua code and directly downloaded Lua's dynamic library and execution file.

Link address:

C/C ++ calls the Lua dynamic library and header file address (used for parsing C/C ++ embedded Lua scripts ):

Http://sourceforge.net/projects/luabinaries/files/5.1.4/Windows%20Libraries/lua5_1_4_Win64_dll8_lib.zip/download (download this version needs to be compiled under vs2005)

You can also go to http://luabinaries.sourceforge.net/download.htmlor http://www.lua.orgto find your desired region.

Lua script parser address:

Http://en.sourceforge.jp/frs/g_redir.php? M1_jaist&f;%2fluabinaries%2f5.2%2fexecutables%2flua-5.2_win32_bin.zip

1. "Hello World"

Haha, first use the classic Hello world to bring the door

1. Open the DOS window in "run" Type CMD and jump the current directory to the Lua directory (My is in c: \ lua-5.2_Win32_bin)

2. Create and add the zookeeper file (test.txt) under the Lua directory.

Print ("Hello World ")

3.go back to the DOS window and enter the execution command lua52.exe test.txt

 

Ii. Cyclic statements

Add in test.txt

For I = 1, 10, 2 do

Print ("I =" .. I)

End

Input the command lua52.txt test.txt in the DOS window.

Note: In the for statement, I = 2, 10, and 2 indicate that the period starts from 1, increases by 2 each time, and ends at 10 (including 10)

 

Iii. Variables

Lua variables do not need to be declared.

A = 5

B = "hello"

Print (a .. "" .. B)

The printed result is as follows:

 

Iv. Table

The data structure and array of Lua can be implemented by table. The instance code is as follows:

AA = {}

AA ["XX"] = 11

AA [3] = 22

Print (AA ["XX"] .. "--" .. AA [3])

Output result:

 

V. Functions

The function Syntax of Lua is

Function Name (parameter)

Implementation...

End

Below is an example

Function add (A, B)

Sum = a + B

Return sum

End

Print ("10 + 20 =" .. add (10, 20 ))

The execution result is as follows:

 

6. C/C ++ calls Lua scripts

1. Create a project

(1). Use vs2005 to create a C ++ console Project

(22.16.unzip the downloaded lua5_4154_win64_dll8_lib.zip to the project.

(3 ). add the dependency on lua51.lib to the project. You can directly write it in the Code, for example, # pragma comment (Lib, "lua51.lib"), and export it to the dependent project set in the project.

 

2. C/C ++ call Lua Process

C/C ++:

/* Open Lua */

Lua_state * l = lua_open ();

/* Open all databases */

Lual_openlibs (L );

/* Execute the Lua script and call the Lua information */

...

/* Disable Lua */

Lua_close (g_l );

 

 

3. Create the test.txt text and write the following code

Function add (A, B)

Sum = a + B

Return sum

End

 

A1 = "Hello World"

B1 = 50

 

4. Write the following test code in the VC project, with more detailed comments

# Include "stdafx. H"

# Include <windows. h>

# Include <iostream>

Extern "C "{

# Include "Lua. H"

# Include "lualib. H"

# Include "lauxlib. H"

}

 

# Pragma comment (Lib, "lua51.lib ")

 

Lua_state * g_l;

 

/* Call the Add function of Lua */

Int lua_add (int A, int B)

{

Lua_getglobal (g_l, "add ");

Lua_pushnumber (g_l, );

Lua_pushnumber (g_l, B );

 

Lua_call (g_l, 2, 1 );

 

/* Get the result */

INTSUM = (INT) lua_tonumber (g_l,-1 );

Lua_pop (g_l, 1 );

Returnsum;

}

 

Int _ tmain (INT argc, _ tchar * argv [])

{

/* Open Lua */

G_l = lua_open ();

/* Open all databases */

Lual_openlibs (g_l );

 

/* Execute the Lua script and pass it in directly through a string */

Lual_dostring (g_l, "Print (100 )");

/* Execute the Lua script to read the configuration file */

Lual_dofile (g_l, "test.txt ");

 

/* Call the Add function of Lua */

STD: cout <"calllua add (1, 2) =" <lua_add (1, 2) <STD: Endl;

/* Obtain the B1 value */

Lua_getglobal (g_l, "B1 ");

If (lua_isnumber (g_l,-1 ))

{

STD: cout <"b1 =" <lua_tonumber (g_l,-1) <STD: Endl;

}

 

/* Obtain the A1 value */

Lua_getglobal (g_l, "A1 ");

If (lua_isstring (g_l,-1 ))

{

Intnlen = 0;

STD: cout <"a1 =" <lua_tostring (g_l,-1, nlen) <STD: Endl;

}

 

/* Disable Lua */

Lua_close (g_l );

Return0;

}

Execution result:

100

Call Lua add (1, 2) = 3

B1 = 50

A1 = Hello World

 

In summary, it takes a lot of time to learn and learn more carefully. In general, this language is faster and easier to learn. Without practical project experience, you cannot fully understand its advantages.

 

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.