Daily basic knowledge of Lua (1)

Source: Internet
Author: User

After installing Lua, you can learn how to program Lua scripts.

1. The terminal executes Lua test. Lua to execute the test. Lua script;

2. After the terminal executes LUA-I test. Lua and runs test. Lua, it enters the interaction mode;

3. run dofile ("test. you can run test. lua. A common editing mode is to open a debugging window on the left, enable Lua interactive mode to run the program using dofile, and open an editor to edit the program. lua file;

4. Lua is case sensitive;

5. "--" is a single-line comment, which is like the following:

--[[print(10)-- no action (comment)--]]

6. It is allowed to reference uninitialized values in Lua, And it returns nil;

7. As long as a variable is declared in the global part, it is a global variable. If you want to destroy a global variable, assign it to nil;

8. You can use the equal sign expression to use Lua as a computer, for example:

> =math.log10(1000)3> =math.pi3.1415926535898> =math.sin(3.14)0.0015926529164868> 

9. Data Type

Lua has the following data types:

> print(type("Hello world"))string> print(type(10.4*3))number> print(type(print))function> print(type(type))function> print(type(true))boolean> print(type(nil))nil> print(type(type(X)))string

The NIL type variable only has one value, that is, nil.

In addition, Boolean has only two values: true and false. In condition determination, except false, nil can represent "no", and other values can only represent true, including 0. For example:

> if 0 thenprint("0 is true!")end0 is true!

10. Features of the number type:

There is no integer type and only a real type. In Lua, a number type can represent any 32-bit integer without rounding errors.

11. Features of the string type:

The memory is automatically allocated and cannot be changed after initialization.

Lua is very effective in managing long strings.

12. type conversion

Automatic type conversion occurs between the number and string classes. For example, when a numeric operation is performed between the string and number classes, the string type is automatically converted to the number type, of course, it can be converted.

> print("10+1")10+1> print("11"+1)12> print("11"*1.2)13.2> 

The displayed types are tonumber and tostring. For example:

> a="10"> print(a)10> print(type(a))string> b=tonumber(a)> print(type(b))number> print(b)10> c=tostring(b)> print(type(c))string> print(c)10> print(#c)2

Print (# C) refers to the length of the print string C.

13. Table

Table is the most important data type in Lua.

Table can be used as a common array, symbol table, set, record, queue...

All tables can access the value using different types of indexes. When a new entry needs to be accommodated, the table will automatically grow.

When referencing an element not initialized in a table, Nil is returned. When NIL is assigned to an element of the table, it is deleted.

Table usually uses 1 as the index start value.

The following is a simple example to obtain the minimum value in the table:

a={}for i=1,5 doa[i]=io.read()endmin=a[1]for i=1,#a doif min>a[i] thenmin=a[i]endendprint("min:")print(min)

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.