Lua scripting language Starter notes _lua

Source: Internet
Author: User
Tags arithmetic operators function examples php language logical operators lua mul

What is LUA

Lua is a compact scripting language. is a research group in the Catholic University of Rio de Janeiro, Brazil (Pontifical Catholic University Rio de Janeiro), by Roberto Ierusalimschy, Waldemar Celes and Luiz Ique de Figueiredo was formed and developed in 1993. It is designed to embed applications, providing flexible extensions and customization capabilities for applications. LUA is written by standard C and can be compiled and run almost on all operating systems and platforms. LUA does not provide a powerful library, which is determined by its positioning. So Lua is not suitable as a language for developing stand-alone applications. Lua has a concurrent git project that provides just-in-time compilation capabilities on a specific platform.

The Lua script can be easily invoked by C + + code, or it can call C + + functions in turn, allowing Lua to be widely used in applications. Not only as an extension script, but also as a normal configuration file, instead of Xml,ini and other file formats, and easier to understand and maintain. LUA is written by standard C and is simple and graceful, and can be compiled and run almost on all operating systems and platforms. A complete LUA interpreter but 200k, Lua is the fastest in all of the current scripting engines. All of this determines that LUA is the best choice for embedded scripting.

Notes in Lua

Single-line Comment

Use two minus signs to indicate the beginning of the comment, extending to the end of the line. Equivalent to the "//" in C language.

Copy Code code as follows:

--Here is a line of comments
Print ("Hello Lanou")

Multiline Comment

Use "--[[" to indicate the start of the comment and use "]" to indicate the end of the comment. This annotation is equivalent to the "*" and "* *" in C language.

Copy Code code as follows:

--[[This is the first line of comment
Here is the second line of comments]]
Pring ("Hello Lanou")

The variables in LUA

By default, LUA considers all variables to be global variables. If you need to define a local variable, you need to use the locals description when declaring the variable.

And when assigning values, allow multiple variables to be assigned at the same time.

Copy Code code as follows:

--I is a local variable
Local i = 1

--Name is a global variable
Name = "Lewis"

--Age,height are all local variables
Local age,height = 34,183.5

--Gender,company are all global variables
Gender,company = "Male", "Blue Gull"

The operators in Lua

Arithmetic operators

Copy Code code as follows:

--+ (addition)
Print (1 + 2)

---(subtraction)
Print (1-2)

--* (multiplication)
Print (1 * 2)

--/(Division)
Print (1/2)

--^ (exponentiation)
Print (1 ^ 2)

Comparison operators

Copy Code code as follows:

--< (less than)
Print (1 < 2)

--> (greater than)
Print (1 > 2)

--<= (less than equal)
Print (1 <= 2)

-->= (greater than or equal to)
Print (1/2)

--= = = (equal to equal to)
Print (1 = 2)

--~= (not equal to)
Print (1 ~= 2)

logical operators

The use of logical operators differs greatly from the C language.

In the Lua language, only false and nil are false, and any other data is true,0 and true!!!

The operations of and and or are not true and false, but are related to his two operands.

A and B: If A is false, return a; otherwise, return B;

A or B: If A is true, return a; otherwise, return B;

Copy Code code as follows:

--and (with)
Print (1 and 2)

--or (or)
Print (1 or 2)

--Not (non)
Print (1 not 2)

With this feature in Lua, you can simulate the?: operator in the C language

For example: X=a?b:c in the C language, in Lua, can be written as x = A and B or C.

Data types in Lua

Key words Describe
Nil Null value, all unused variables are nil;nil both value and data type
Boolean Boolean type with only two valid values: True and False
Number Numeric type, in Lua, the equivalent of a double in the C language.
String String, if you like, a string that can contain a "." Character
Table Relationship type, this type of function is more powerful
function function type, you can declare variables by function type
UserData This type is specifically designed to deal with Lua hosts. Hosts are usually developed by the C and C + + languages, in which case UserData can be any type of host, commonly used in structure and pointer type
Thread Thread type, there is no real thread in Lua.

A block of code in LUA

In c, code blocks are enclosed in "{" and "}", and in Lua, using the Do and end keywords.

Copy Code code as follows:

Todo
Print ("Hello")
End

Types of relationships in Lua

Relationship type, is a very powerful type. This type is similar to the mapping structure in the C + + language, similar to the array object in the PHP language, similar to the Nsdictionary object in the OC.

The definition of a relational type (Table) in Lua is simple, and its main feature is to use "{" and "}" to enclose a series of elements.

Copy Code code as follows:

--Declares a global variable table of a relationship type
Table = {}

--The value of the Assignment Relation variable table index to 0 o'clock is 34
Table[0] = 34

Can also be written as Table.name = "Lewis"
table["Name" = "Lewis"
Table.gender = "Male"

-You can also write an object indexed as "son" into another table
Table["Son"] = {name = "DD", gender = "Male"}

--When you access an object, you can use the
Print (Table.son.name)

You can also declare the following

Copy Code code as follows:

--Declaring the Relationship variable table
Table = {
,--equivalent to [1] = 10
[100] = 40,


Lewis = {-can also be written ["Lewis"] =
Age = 34,--can also be written ["age"] = 34
gender = "Male",
},

20,--equivalent to [2] = 20
}

Print (table[2])

The functions in Lua

In Lua, the definition of a function is very simple. However, it is important to note that the return statement must be written before end. If we have to add a return statement to the function, it should be written.

Copy Code code as follows:

Todo
--return statement must be before the end of a block of code
Return
End

function examples

Copy Code code as follows:

--declaring function type variable sum
function sum (V1,V2)
--Function body
Return V1 + v2
End

--the same function variable can also be declared as follows
Mul = function (V1,V2)
--Function body
Return V1 * v2
End

--Call the first function sum
Print (sum (2,3))
--Call the second function mul
Print (Mul (3,4))

The classes in Lua

As I said before, table types can have any type of value, including functions!

So, we can create a table with function variables.

Copy Code code as follows:

Lewis = {
Age = 34,
Work = function (Self,message)
--Function body
Self.age = self.age + 1;
Print (self.age.. message)
End
}

Print (Lewis.age)
Lewis.work (Lewis, "work")

--You can also call methods as follows
Lewis:work ("Work")

End

Like C, LUA provides a number of standard library functions to enhance language functionality. Using these functions, you can easily manipulate a variety of data types.

Refer to the book "Programming in Lua".

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.