Lua Data Type Introduction _lua

Source: Internet
Author: User

Lua is a powerful, fast, lightweight, embedded scripting language, implemented by standard ANSI C, with a strong set of features and an easy-to-use C API, it makes it easy to embed or expand into other languages, and has an unofficial claimed Chinese name-"Go-ah".

Installing Lua

Lua installation is very simple, the source download down, direct make on the line:

Copy Code code as follows:

wget http://www.lua.org/ftp/lua-5.2.2.tar.gz
TAR-ZXVF lua-5.2.2.tar.gz
CD lua-5.2.2
Make generic
Make install
Lua-e ' Print ("Hello world!") '

If it is a MAC user and is equipped with Homebrew, an instruction is on the line:

Copy Code code as follows:

Brew Install LUA

Lua is a dynamic (weak) type of language that has several data structures:

Nil (empty)

The nil type represents a value that has no valid value--nil, such as printing a variable with no assignment, and then outputting a nil value:

Copy Code code as follows:

Print (Type (a))

For global variables and Table,nil there is also a "delete" effect, to the global variable or table variables to assign a nil value, equivalent to delete them, execute the following code to know:

Copy Code code as follows:

Tab1 = {Key1 = "val1", Key2 = "Val2", "Val3"}
For K, V-pairs (TAB1) do
Print (K.. " - " .. V
End

Tab1.key1 = Nil
For K, V-pairs (TAB1) do
Print (K.. " - " .. V
End

Boolean (Boolean)

The Boolean type has only two optional values: True (True) and False (false), and Lua regards false and nil as "false" and the other is "true"

Copy Code code as follows:

Print (Type (true))
Print (Type (false))
Print (type (nil))

If type (false) or type (nil) then
Print ("False and nil are false!")
Else
Print ("Other is true!")
End

Number (digit)

Lua defaults to only one type of number-double (the default type can modify the definition in luaconf.h), and the following are all considered number types:

Copy Code code as follows:

Print (Type (2))
Print (Type (2.2))
Print (Type (0.2))
Print (Type (2e+1))
Print (Type (0.2e-1))
Print (Type (7.8263692594256e-06))

String (strings)

A string is represented by a pair of double quotes or single quotes

Copy Code code as follows:

string1 = "This is string1"
string2 = ' This is string2 '

You can also use 2 square brackets "[[]]" to denote a "piece" string

Copy Code code as follows:

HTML = [[
<body>
<a href= "http://jb51.net/" >just a link</a>
</body>
]]
Print (HTML)

When you perform arithmetic operations on a numeric string, Lua attempts to turn the numeric string into a number

Copy Code code as follows:

Print ("2" + 6)
Print ("2" + "6")
Print ("2 + 6")
Print (" -2e2" * "6")
Print ("error" + 1)

Use.. Connection string

Copy Code code as follows:

Print ("a" ...). ' B ')
Print (157.. 428)

Use # as the length operator to get the length of the string before the string

Copy Code code as follows:

Len = "Length"
Print (#len)
Print (# "Length")

Table (Tables)

In Lua, the creation of a table is done through a "construct expression", the simplest constructed expression is {}, used to create an empty table. You can also add some data to the table to initialize the tables directly:

Copy Code code as follows:

--Create an empty table
Local TBL1 = {}

--Direct Initial table
Local TBL2 = {"Apple", "pear", "orange", "Grape"}

The table in Lua is actually an associative array (associative arrays) in which the index of an array can be a number or a string

Copy Code code as follows:

A = {}
a["Key" = "value"
Key = 10
A[key] = 22
A[key] = A[key] + 11
For K, v. in pairs (a) do
Print (K.. " : " .. V
End

Unlike other languages, arrays take 0 as the initial index of the array, and in Lua the default initial index of the table starts with 1.

Copy Code code as follows:

Local TBL = {"Apple", "pear", "orange", "Grape"}
For key, Val in pairs (TBL) do
Print ("Key", key)
End

The table's variable is just an address reference, and the action on the table does not produce a copy of the table or create a new table

Copy Code code as follows:

A1 = {}
a1["Key" = "Val1"
a2 = A1
Print (a2["key"])
a2["Key" = "Val2"
Print (a1["key"])
Print (A1.key)

Table does not have a fixed length, the table length automatically grows when new data is added, and no initial table is nil

Copy Code code as follows:

a3 = {}
For i = 1, the Do
A3[i] = i
End
a3["Key" = "Val"
Print (a3["key"])
Print (a3["None"])

function (Functions)

In Lua, a function is considered a "first class value" (first-class value), and a function can exist in a variable

Copy Code code as follows:

function Factorial1 (n)
if n = = 0 Then
Return 1
Else
return n * FACTORIAL1 (n-1)
End
End
Print (Factorial1 (5))
Factorial2 = Factorial1
Print (Factorial2 (5))

function can be passed by parameter in the form of an anonymous function (anonymous function)

Copy Code code as follows:

function Anonymous (tab, fun)
For K, V in Pairs (tab) do
Print (fun (k, V))
End
End
tab = {Key1 = "val1", Key2 = "Val2"}
Anonymous (tab, function (key, Val)
Return key ... " = " .. Val
End

Thread (Threads)

In Lua, the main thread is the collaboration program (Coroutine). Like thread, it has its own independent stack, local variables, and instruction pointers, and can share global variables and most other things with other collaborative programs.

The difference between a thread and a coprocessor: A thread can run multiple times, and a coprocessor can only run at any one time, and a suspend in a running state is paused only when it is suspended.

UserData (custom type)

UserData is a user-defined data that represents a type that is created by an application or C + + language library, and can be invoked in a Lua variable by storing data of any data type of any C + + (typically struct and pointers).

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.