http://blog.csdn.net/eric_xjj/article/details/9209947
In Lua, the and logical operator returns FALSE if the first argument is false, does not execute the code of the second argument (even if the second argument is an incorrect expression, and runs smoothly); If the first argument is true, the value of the second parameter is returned.
An OR operation is similar to and
In Lua, variables are global by default, which often leads to some debugging difficulties, and it is best to explicitly add the local keyword before the variable name to declare the variable as a local variable.
Gnumber = Ten --This is a default global variable print (type (gnumber)) --The output is number Gnumber = nil--Before the number type Gnumber = 10 variable is released print (Type (gnumber)) -output is nil function localvarfunction () local ptable = {}-- Declare a local variable with the local keyword, which will be sold after executing the Localvarfunction method ... End
table can also use other values as index values, and can be used as an index to the same table as numbers and other values
gTable = {} gtable.name = "Eric" Gtable.gender = "man" gtable.phonenumber = "0000000000" gtable[1] = "Company " gtable[2] =" department " for Index, value of pairs (gTable) do print (index, value) end --[[ output is as follows: C11/>1 Company 2 Department phonenumber 0000000000 gender man name Eric --]]
Use "" for numbers, not for numbers. dot
The index of a table can also be the table itself, which makes up a multidimensional table or multidimensional dictionary.
gTable = {} Gtable.name = "Eric" Gtable.gender = "man" Gtable.phonenumber = "0000000000" gtable[1] = "Company" gtable[2] = "department" Gtable.hobby = {"Run", "reading", "Game", "anime"}--multidimensional table, can be accessed by gtable.hobby[1]. That is Gtable.ho BBY itself is also a table gtable.sectable = {} gTable.secTable.job = "programmer" GTable.secTable.label = "Write code" gTable. Sectable.description = "Responsibility is to implement product logic" for index, value in pairs (gTable) do print (index, value) if ("table" = = Type (value)) then-for-IDX, var in pairs (value) do print ("Two-dimensional table:", IDX, Var) End END end--[[output is as follows: 1 Company 2 Department door Hobby table:0x7fdceac14bc0 two-dimensional table : 1 running two-dimensional table:2 reading two-dimensional table:3 game two-dimensional table:4 anime PhoneNumber 0000000000 Gende R man sectable table:0x7fdceac15100 two-dimensional Table:label writing code two-dimensional table:description responsibility is to achieve the logic of the product two Dimension Table:job ProgrammerName Eric--]
The table.getn () function returns the number of elements in the table. Such as:
- Gstringtable = {"A", "B", "C", "D", "E"}
- For i = 1, table.getn (gstringtable) does
- Print (Gstringtable[i])
- End
As you can see, the following table of the default table is represented by numbers
In a LUA script, the function starts with the function keyword, then the name of the functions, the argument list, and finally the end keyword . It is important to note that the arguments in the function are local variables if there is (...) in the argument list. , LUA creates a local variable arg of type table, which holds all arguments passed at the time of invocation and the number of arguments (ARG.N).
function Printfriendinfo (name, Gender, ...) Local friendinfostring = String.Format ("name:%s gender:%d", Name,gender) if 0 < ARG.N then for index = 1, a RG.N do friendinfostring = String.Format ("%s otherinfo:%s", friendinfostring, Arg[index]) End End Print (friendinfostring) end Printfriendinfo ("Eric", 1, "Programmer", "2b")--The output is: --Name:eric gender:1 otherinfo: Programmer otherinfo:2b otherinfo:50
Here arg.n=3
The return value of the LUA function is compared with other languages, and it is special to be able to return multiple return values
function GetUserInfo () local name = "Eric" local gender = 1 local hobby = "Anime" return name, gender, H Obby End print (GetUserInfo ()) -output Result: Eric 1 Anime
the LoadString (pstring) () function can execute a LUA generation that consists of pstring strings directly but does not provide the error function .
dofile (filename) function is to load and execute Lua immediately script file . A file or data file that can be used to load a defined function, or a LUA code that executes immediately . The Dofile function takes the execution directory of the program as the current directory. If you want to load a file in a subdirectory of the program execution directory, you need to add the path to the subdirectory.
- Dofile ("/users/ericli/workspace/lua language/hellolua.lua")
- --output Result: Hello lua!
Quick Start for LUA basics