Types and values of LUA

Source: Internet
Author: User

Types and values of LUA

(i) Basic introduction

Lua is a dynamic type of language, and variables do not need to be defined. There are eight types in Lua, namely

Nil                 -- null boolean-           -  boolean number--              digit string              --  String UserData-            -  custom function            --  function thread              --  thread table               -- Table  

Use the type () method to identify the type of the variable.

1 Print(type(Nil))--> Nil2 Print(type(true))--> Boolean3 Print(type(1314))--> Number4 Print(type("Hello World"))--> String5 Print(type(Print))--> Function6 Print(type(type(A))  --> The String,type method returns a value of type string 

There are no pre-defined variables, and each variable can be of any type. Next look at the following example:

1 Print(type(a))--> Nil (a not initialized)2A =Ten             3 Print(type(a))--> Number4A ="a string!!"5 Print(type(a))--> String6A =Print          7 Print(type(a))--> Function

In the above example, variable a switches between different types without error, but it is not recommended to do so, which can cause confusion. There is, however, a special case, nil. Assigning a variable to nil in Lua is equivalent to deleting the variable. In addition, the last two lines tell you that function is actually a variable in Lua, which is described in detail next.


(ii) Nil
The default value for a global variable before the first assignment is nil. LUA uses nil to identify "invalid value" cases.

1 local a = {}2printnil)      --> False

This is the author recently stepped on a small pit, in the Lua Hollow table is not equal to nil, was stupid to cry ...


(iii) Boolean
There are two optional values in the Boolean: True and False. It is important to note that only false and nil are "false" in Lua and that the rest are "true", so Lua considers 0 and the empty string to be true, which is different from other languages.

(d) Number
The number type has two internal representations: integers and floating-point numbers. As for when to use the internal form, LUA has a clear rule and is automatically converted internally as needed (see http://cloudwu.github.io/lua53doc/manual.html#3.4.3). So programmers can choose to ignore the difference between integers and floating-point numbers in most cases.

(v) string

A string in Lua refers to the "sequence of characters". You can use it in Lua, but because the string is represented by a line or double quotation mark, select a uniform use in the actual development process.

1 ' Hello World ' 2 " Goodbye World "

When a string is multiple rows, you can use the "[[]]" notation to define a multiline string, and Lua does not interpret the escape sequence.

The string for LUA is immutable. Take a look at the following lines of code:

 1  a =  "  hello World   " 2  print  (a) -- > "Hello World"  3  a =  " goodbye World   " 4  print  (a) -- 

There may be friends who use this example to prove that the string is subject to change. Note that in the example above, a is just a reference to the string object "Hello World", and the next assignment statement simply changes the reference to variable a, and does not change "Hello World" itself.

As with other objects, Lua automatically allocates and frees memory.

During the run, Lua automatically converts the type between string and number, as to whether string is converted to number or number to string depends on the specific operation. When the arithmetic operator is used between them, the string is converted to number

1 Print("Ten"+1)-->2 Print("Ten + 1")--> + 13 Print("5"*"2")--> -104 Print("Hello"+1)--ERROR (Cannot convert "hello")

When you use string related operations between them, number is converted to a string, such as a join operation for strings

1 Print (ten)      -- > 1020

(vi) Table
Table is a very powerful type in Lua, unlike the C language, where table can be indexed not only by integers, but also by strings or other types of values (except nil). In addition, table does not have a fixed size and can be dynamically added to a table with arbitrary elements. (PS: The dynamic addition of table is not recommended in fact, LUA author an article mentions that the dynamic addition of table will reduce the code efficiency, http://www.lua.org/gems/sample.pdf)


In Lua, table is neither a value nor a variable, but an object. You can think of a table as a dynamically allocated object, with only one team of their references (pointers) in the program. The creation of a table is done through the construction of an expression, and the simplest constructor expression is {}.


Table is anonymous, and a variable referencing table does not have a fixed associativity on the table. And when a table is not referenced by any variable, it starts the garbage collection mechanism to remove it and frees up memory.

When the table is not initialized, his content is nil, and as with global variables, nil is given to a variable in the table to delete the element. In Lua, this form of a["name" provides a simpler way to

A.name

This is easy to write, but easily confusing to novice programmers. In fact, a["x" means the table is indexed with the string "x", whereas A[x] is indexed by the value of X. The following code can explain:

 local  a = {}  2  x =  " y  "   a[x] = 10  4  print  (a[x]) >10 equals a[" y "]      5  print  (a.x) -- >nil equals a["x"]      6  print  (A.Y) -- >10 equals a["y"]   

In Lua5.1, the long operator "#" is used to return the last index value of an array or linear table. In actual projects we often use this operator to get the length of an array or linear table. However, the use of this operator is a pit, such as the following code:

1 local a = {}2 a[13print(#a)

In Lua, the result of all uninitialized element indexes is nil. Lua takes nil as a marker for defining arrays. When an array has "voids", that is, when the middle contains nil, the length operator considers these nil elements to interpret the trailing sign. Because of the a[1]=nil, the output for the above code should be 0. Therefore, this is a problem that needs to be considered when working with table.

(vii) function
Functions in Lua are the first class of values, that is, both as parameters and as return values, and to other variables. LUA can call functions written in its own Lua language, and can call functions written in the C language. All of Lua's standard libraries are implemented in C, including string libraries, table libraries, I/O libraries, OS libraries, arithmetic libraries, and debug libraries.

(eight) UserData and thread
UserData, as the name implies, is the meaning of user-defined data. It is used to identify a new type created by the application or the C language library. Because the UserData type can store any C-language data in a LUA variable. Pre-defined operations UserData in LUA have only the assignment and equality comparisons.
Thread is mainly used for collaborative programs and will be explained in detail later.

Types and values of 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.