Lua scripts quick start
Lua is case sensitive
premise
After Lua is installed on this machine, the installation steps of lua can be completed
Basics
notes
-- This is a single line comment
-- [[ This is a multiline comment This is a multiline comment -- ]]
Reserved keywords
Reserved keyword document address
variable
Type when variable is undeclared and uninitialized
When the variable is undeclared or declared uninitialized, the default value is nil
local variable
Variables are decorated with the local keyword, such as
-- This is a local variable local a = 10
function max() -- This is also a local variable local a = 10 end
global variable
If there is no local keyword, it is a global variable, such as
-- This is a global variable a = 10
Note: even variables in the function are global variables as long as they are not decorated with the local keyword
function max() a = 10 end
Where are global variables stored?
Saved in a table table, Relevant_ Some descriptions of G
What are the effects of not deleting global variables?
At present, we don't see the impact of not deleting global variables in official documents and books. and book It is also proposed that users do not need to care about Lua's memory, and Lua's memory management is automatic
How to delete global variables?
Assign nil to the global variable
a = 10 -- Delete global variable a = nil
Multiple variable initialization
The basic format of multiple variable initialization is: multiple variables are on the left of the equal sign, and the corresponding values are on the right of the equal sign
local a, b = 10, 20
When the number of values on the right is less than the number of variables on the left, no corresponding value is found. The variable is the default value nil in lua
a, b = 10
When the number of values on the right is more than the number of variables on the left, the more values will be automatically ignored
a, b = 10, 20, 30
data type
Determine what type a variable is and use type
type(123)
-
nil null
nil should be quoted when making comparisons
type(X) == 'nil'
-
boolean
-
Number double precision floating point number
-
string single quotation mark or double quotation mark
Use # to calculate string length
local string str = 'message' print("The string length is:", #str)
Another common way to write concatenated strings
local msg1 = 'msg1' print(msg1 .. 'append' .. 'builder')
Note that strings written in this way can only be local variables
String common operation documents , including search, case conversion, interception, splicing, etc
-
function
Lua's function can return multiple values separated by commas
function max() a, b = 10, 20 return a, b end -- Call function max()
Call after function declaration
-
table
There are several other types that are not listed because they are temporarily unavailable in the current work
Conditional expression
if
local a, b = 10, 10 if (a == b) then print("if expression") end
if else
local a = 10 if (a == c) then else print("if-else expression") end
If else if nesting
local a, b = 10, 10 if (a == c) then elseif (a == d) then elseif(a ==b ) then print("if-elseif expression") end
array
Array declaration
array = {'index1', 'index3'}
loop
while Loop
a = 10 while (a < 20) do a = a+1 if (a > 18) then break end print("value of a:", a) end
for loop
for i = 10, 1, -1 do print("value of i:", i) end
Combined with array, use the built-in ipairs function
local array = {'jack', 'tom', 'bob'} for k, v in ipairs(array) do print("This cycle printing value: ", v) print('This cycle printing key:', k) end
repeat... until loop
repeat is equivalent to do while. In any case, it will execute a loop at least once
b = 10 repeat b = b+1 print("value of b:", b) until(b > 15)
error handling
At present, this part has not been handled
Calling Redis in Lua script
Use redis call
local rc, rl, ll, key, value = redis.call, redis.log, redis.LOG_NOTICE, KEYS[1], ARGV[1] local result = rc("SETEX", key, 60, value) print("result value is:", result)
function library
Lua built-in function quick query
stay The list of class libraries at the bottom of the official document You can quickly find functions you don't know and understand, and Lua standard library listed in the official tutorial
resources
Sample code
this paper Sample code
file
- Lua official documents
- Tutorial on Lua on tutorialpoint
- Books on Lua programming on the official website
IDEA plug-in
- EmmyLua , it is recommended to install the plug-in to facilitate Lua's syntax prompt and debug debugging