Lua basic syntax (1)

Since redis + lua can do a distributed lock it simple to learn, I feel a bit like grammar and python

Scripted Programming

We can Lua holding program code to a file ending lua and performs the programming mode is called a script, such as the following code will be stored in a script file named hello.lua of:

print("Hello World!"

Use the above script lua executive, output is:


Note

Single-line comments

Minus two is a single line comment:

--

Multi-line comments

- [[ multi-line comment multi-line comments -]]
 
  


Identifier

Lua identifier used to define a variable, user-defined functions for additional items. After the identifier to a letter A to Z a to z, or the beginning or underscore _ plus zero or more letters, digits, (0 to 9).

Best not to increase the use underscores to write letters identifier, because Lua reserved words is the same.

Lua is not allowed to use special characters such as @, $, and% defined identifier. Lua is a case-sensitive programming language. Therefore, in Lua Runoob with runoob are two different identifiers. Here are some correct identifier:

mohd         zara      abc     move_name    a_123
myname50     _temp     j       a23b9        retVal

Key words

Following is a list of reserved keywords Lua. Keywords can not be retained as a constant or variable or other user-defined identifier:

and break do else
elseif end false for
function if in local
nil not or repeat
return then true until
while goto    

The general convention, begin with an underscore connection string of uppercase letters the names (such as _VERSION) are reserved for internal Lua global variables.


Global Variables

By default, variables are always considered to be global.

Global variables need not be declared, after assignment to a variable creates a global variable, access does not initialize a global variable will not go wrong, but the results obtained are: nil.

print(b)
b=10
print(b)

If you want to delete a global variable, simply assign values ​​to variables is nil.

Such variable b as if the same had never been used. In other words, if and only if a variable is not equal to nil, this variable that is present.

Published 407 original articles · won praise 2 · Views 6801

Guess you like

Origin blog.csdn.net/qq_29860591/article/details/104766320