[Lua] Getting Started tutorial, lua getting started tutorial

Source: Internet
Author: User

[Lua] Getting Started tutorial, lua getting started tutorial
What is Lua?

Lua is a small scripting language. It is a research group at the Pontifical Catholic University of Rio de Janeiro in Rio de Janeiro, developed in 1993 by Robert Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo. It is designed to embed an application and provide flexible scalability and customization for the application. Lua is compiled by standard C and can be compiled and run on almost all operating systems and platforms. Lua does not provide a powerful library, which is determined by its positioning. Therefore, Lua is not suitable for developing independent applications. Lua has a GIT project for simultaneous compilation on a specific platform.
Lua scripts can be easily called by C/C ++ code, or call C/C ++ functions in turn, which makes Lua widely used in applications. Not only as an extension script, but also as a common configuration file, instead of XML, ini and other file formats, and easier to understand and maintain. Lua is compiled by standard C. The code is concise and elegant, and can be compiled and run on almost all operating systems and platforms. A complete Lua interpreter is only 200 k. Currently, Lua is the fastest in all script engines. All of these determine that Lua is the best choice for embedded scripts.

Comments in Lua

Single line comment

Use two minus signs to indicate the beginning of the comment and continue until the end of the line. It is equivalent to "//" in C "//".

12 -- Here is a line of commentprint("Hello Lanou")

Multi-line comment

Use "-- [" to start the annotation, and use "]" to end the annotation. This annotation is equivalent to "/*" and "*/" in C "*/".

123 -- [[This is the first line of commentThe second line of comment]pring("Hello Panda")
Variables in Lua

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

You can assign values to multiple variables at the same time.

1234567891011 -- I is a local variable.local i = 1 -- Name is a global variable.name = "Lewis" -- Age and height are all local variables.local age,height = 34,183.5 -- Gender and company are global variables.gender,company = "Male","Lanou"

  

Operators in Lua

Arithmetic Operators

1234567891011121314 -- + (Addition)print(1 2) -- (Subtraction)print(1 2) -- * (Multiplication)print(1 2) --/(Division)print(1 2) -- ^ (Multiplication)print(1 2)

Comparison Operators

1234567891011121314151617 -- <(Less)print(1 2) --> (Greater)print(1 2) -- <= (Less than or equal)print(1 <= 2) --> = (Greater than or equal)print(1 2) -- = (Equal to or equal)print(1 == 2) --~ = (Not equal)print(1 ~= 2)

Logical operators

When using logical operators, it is very different from the C language.

In Lua, only false and nil are false. All other data is true, and 0 is true !!!

And or are not true or false, but related to their two operands.

 

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

A or B: if a is true, a is returned; otherwise, B is returned;

12345678 -- and(And)print(1 and 2) -- or(Or)print(1 or 2) -- not(Not)print(1 not 2)

Through this feature in Lua, can we simulate? : Operator

For example, in C, x =? B: c. In Lua, it can be written as x = a and B or c.

Data Types in Lua
Keywords Description
Nil Null Value. All unused variables are nil. nil is both a value and a data type.
Boolean Boolean type. There are only two valid values: true and false.
Number Numeric type. In Lua, it is equivalent to double in C.
String String. If you want to, the string can contain the "\ 0" character.
Table Link type, which has powerful functions
Function Function type. variables can be declared by the function type.
Userdata This type deals with the Lua host. Generally, the host is developed by c and c ++. In this case, userdata can be of any type of the host, and the common types are struct and pointer.
Thread Thread type. There is no real thread in Lua.
Code block in Lua

In C, the code block is enclosed by "{" and "}". In Lua, the do and end keywords are used.

123 doprint("Hello")end
Link Type in Lua

Link type, which is a very powerful type. This type is similar to the C ing structure in c ++, similar to the array object in PHP, similar to the NSDictionary object in OC.

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

123456789101112131415 -- Declare a global variable table of The Link Typetable = {} -- The table index of the value assignment relation variable is0The value is34table[0] = 34 -- You can also write table. name ="Lewis"table["name"] = "Lewis"table.gender = "Male" -- You can also set the index"son"The object is written into another table.table["son"] = {name = "DD",gender = "Male"} -- Can be used to access objectsprint(table.son.name)

You can also declare

123456789101112131415 -- Declare the relational variable tabletable = {    10, -- Equivalent [1] = 10    [100] = 40,    Lewis = {-- can also be written ["lewis"] =         age = 34, -- Can also be written ["age"] = 34        gender = "male",    },     20, -- Equivalent [2] = 20} print(table[2])
Functions in Lua

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

1234 do--returnThe statement must be before the end of a code block.returnend

Function example

12345678910111213141516 -- Declare the function type variable sumfunction sum( v1,v2 )    -- Function body    return v1 + v2end -- The same function variable can be declared as follows:mul = function (v1,v2 )    -- Function body    return v1 * v2end -- Call the first function sum.print(sum(2,3))-- Call the second function mulprint(mul(3,4))
Classes in Lua

The front side once said that the table type can have any type of values, including functions!

Therefore, we can create a table with function variables.

1234567891011121314 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,"Go to work") -- You can also call the following method:lewis:work("Go to work")

 

End

Just like the C language, Lua provides many standard function libraries to enhance language functions. With these functions, you can easily operate on various data types.

You can refer to Programming in Lua.

Related Article

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.