LUA Learning Notes (i)

Source: Internet
Author: User
Tags lua sin

Lua run environment is relatively simple to build, you can go to the official website to view http://www.lua.org/download.html
1.1, Chunks
Each line in a file or interactive mode is a chunks, a chunks can be a statement, a series of statements, or a function, MByte chunk is common in Lua.
Lua-i requires LUA to run the specified chunk after entering interactive mode
Another way to connect an external chunk is to use the Dofile function, dofile the function to load the file and execute it.
For example:

In interactive mode, do the following:

1.2. Global Variables
Global variables do not need to be declared, the global variable is created after a variable is assigned, and the global variable with no initialization result is nil

When you delete a variable, you only have to assign his value to nil, when the worth of a variable and only if it is not equal to nil exists.
1.3. Lexical conventions
Letters, underscores, and sequences of numbers that begin with a letter or underscore. It is best not to use underscores + uppercase letters. The reserved word for LUA cannot be an identifier as follows:
And,break,do,else,elseif,end,false,for,function,if,in,local,nil,not,or,repeat,return,then,true,until,while
1.4. Command line mode
LUA [Options][script][args]
-e: direct command to LUA

-L: Load a file
-I: Enter interactive mode
_prompt built-in variables as a prompt for interactive mode

Before running, LUA constructs an ARG table with all the parameters. The script name index is 0, and the script parameter increases from 1 onwards. The parameters in front of the script are reduced from 1 onwards.
Lua-e "Sin=math.sin" script a B
Arg[-3]= "Lua"
arg[-2]= "-E"
arg[-1]= "Sin=math.sin"
arg[0]= "Script"
Arg[1]= "a"
Arg[2]= "B"
2. Type and value
Lua is a dynamic language, a variable does not have a type definition, and LUA has 8 basic types: nil,boolean,number,string,userdata,function,thread and table. function type to test the type of a given variable value
For example:

2.1nil
Special type in Lua, he has only one value: nil, a global variable is not copied by default nil, and the global variable is assigned nil to delete the variable
2.2Booleans
Two values of false and all values in True.lua can be used as conditions. Except for false and nil in the control structure condition, the other values are true.
2.3Numbers
Represents a real number, and there are no integers in Lua. Numbers in Lua can handle any long integer without worrying about the error
2.4Strings
Character sequence, Lua is a 8-bit byte, so the string can contain any numeric characters. The string in Lua is not modifiable, you can create a new variable to hold the string you want

Lua can automatically allocate and release memory, a string can contain only one letter can also contain a book, can efficiently handle long strings, 1M is normal
Escaping in Lua:
\b– back
\f– page Change
\n– line break
\r– Enter
\t– watchmaking
\ –”\”
\ "– Double quotation marks
\ ' – Single quote
[– Left Middle bracket
]– Right Middle Bracket
For example:

You can also use [[...]] Represents a string, nesting does not need to be escaped, can be wrapped

Lua automatically transitions between string and numbers at run time

.. In Lua is the string connector, which is written after a number: You must add a space to prevent it from being interpreted incorrectly
Although the string and the value can be automatically converted, but 10== "10" is false, you need to explicitly convert the string using the Tonumber (), if the string is not the correct number function returns nil, conversely, you can convert the number through ToString () to a string.
2.5functions
A function is a first-class value that can be stored in a variable, either as a function parameter or as a function return value.
2.6userdata
UserData can place C data in Lua variables, UserData there are no predefined actions in Lua except assignment and equality comparisons. UserData a new type that is used to describe an application or to create a library using C implementations. For example, use a standard I/O library to describe a file.

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.