当前位置:网站首页>Lua basic grammar learning
Lua basic grammar learning
2022-07-27 23:40:00 【Fruit brother】
Lua Basic grammar learning
Learning links
- https://blog.csdn.net/jiangwei0512/article/details/51057649
- https://blog.csdn.net/THIOUSTHIOUS/article/details/86552762
- https://segmentfault.com/a/1190000018641801
- https://cloud.tencent.com/developer/article/1043931
- https://github.com/openresty/lua-nginx-module
- https://blog.51cto.com/xikder/2331649 Nginx Lua Implementation phase of
1、 notes
Lua The notes used are as follows :
-- Single line comments use
--[[
Multiline comments use
Multiline comments use
]]
--[[
if x > 1 then
-- notes 1
else
-- notes 2
end
--]]
2、 data type
Lua Of 8 Basic types :
Some notes :
- nil Type only nil Such a value
- boolean There are two values true and false. in addition Lua All values in can be used in conditional statements , Besides false and nil Said the false , Everything else means true . such as 0, It means true .
- The value type is only number, No, int、float Other types .
- string You can use double quotes , You can also specify with single quotation marks . You can also use [[ It's a string ]],[[]] It can contain multiple lines , Can be nested , And do not explain the transfer character .
- function Like other types mentioned above , It belongs to the first kind of value , That is to say, it can also exist in ordinary variables .
- table、userdata and thread I'll talk about it later .
3、table library
do
--> table.concat Connect function
tab = {
"a", "c", "d", "b"}
--(1)
-- One parameter , Parameter is surface (tab), The function is to connect tab All of the value
print(table.concat(tab))
--> Output results : acdb
--(2)
-- Two parameters , Parameter one : surface (tab), Parameter two : Separator (nil),
-- The function is to connect tab All of the value,value In between nil separate
print(table.concat(tab, nil))
--> Output results : acdb
--(3)
-- Three parameters , Parameter one : surface (tab), Parameter two : Separator (" @@ "),
-- Parameter 3 : The starting position (2), The function is to connect tab All of the value, The location is from 1 The beginning of the calculation
-- value In between " @@ " separate , from tab The second value Start connecting ,
-- To tab Of end
print(table.concat(tab, " @@ ", 2))
--> Output results : c @@ d @@ b
--(4)
-- Four parameters , Parameter one : surface (tab), Parameter two : Separator (" - "),
-- Parameter 3 : The starting position (2), Parameter 4 : End connection location , The function is to connect
-- Pick up tab All of the value,value In between " - " separate , from tab
-- The second value Start connecting , To tab Of The first 3 individual value End connection
print( table.concat(tab, " - ", 2, 3))
--> Output results : c - d
print(table.concat(tab, " ## \n", 2, 4))
--> Output results :
--> c ##
--> d ##
--> b
--(5)
for i,v in ipairs(tab) do
print(i,v)
end
--> Output results :
-- 1 a
-- 2 c
-- 3 d
-- 4 b
-- explain table.concat Will not change original table namely (tab) Structure
end
4、 expression
- ~= amount to c In language !=, It's not equal to .
- table、userdata and function It's quoting comparison , Only two variables pointing to the same object are equal .
- The logical operator is "and or not", But here and and or The meaning follows c Languages are different :
a and b: If a by false, Then return to a, Otherwise return to b;
a or b : If a by true, Then return to a, Otherwise return to b. - “…” Two points , Represents a character connector ; If the operand is number, Is converted to a string :
print(1 … 2) --> 12, Be careful … You need a space before and after , Otherwise, an error will be reported
Note that there 1 … There is a space between , Otherwise, it will report a mistake .
But if it's a string, you don't need :
print("hello".."world") --> helloworld
5、 The structure of the watch
Here is a basic example :
table1 = {
} --> Empty table
names = {
"jack", "john", "jimmy"} --> List initialization
print(names[1]) --> jack, Subscript from 1 Start
names[4] = "jason" --> Dynamic addition
print(names[4]) --> jason
print(names[5]) --> nil, Because it doesn't exist
a = {
x = 0, y = 1} --> record Form initialization
print(a.x) --> 0
print(a["x"]) --> 0, Another way to express
b = {
"helloworld", x = 1, "goodbye"} --> Mixed form
print(b[1]) --> helloworld
print(b.x) --> 1
print(b[2]) --> goodbye
-- print(b.1) --> There's no such thing
Commas can be used to separate tables , You can also use semicolons .
There is a more general form :
a = {
["+"] = "add", ["-"] = "sub", ["*"] = "mul", ["/"] = "div", } --> There can be a comma after
print(a["+"]) --> add
b = {
[1] = "one", [2] = "two", [3] = "three"}
print(b[1]) --> one
6、 Control statement
IF sentence
if xxx then xxx end
if xxx then xxx else xxx end
if xxx then xxx elseif xxx then xxx else xxx end
elseif There can be many , Be careful else and if There is no space in the middle !!
WHILE sentence
while xxx do xxx end
REPEAT-UNTIL sentence
repeat xxx until xxx
FOR sentence
for var=exp1,exp2,exp3 do xxx end
here for The sentence inside means var With exp3 by step, from exp1 To exp2.
There are a few points to pay attention to :
- How many? exp It will only run once , And in for Before the cycle ;
- var It's a local variable ;
- Don't change during the cycle var.
for x,y,z in xxx do xxx end
BREAK and RETURN sentence
break Exit the current loop ;return Exit the function and return the result .
Be careful break and return Can only be used in one block Ending . If you really want to use it in another place sometimes , In this way :
do break/return end
7、 function
Definition of function :
function funcName (args)
states
end
About function calls , If there are no parameters , Need to use ();
If the parameter is a string or table construction , Don't have to ().
Lua The matching of real parameters and formal parameters in functions is also less particular , Actual parameters are more than formal parameters , The redundant part will be ignored ; Fewer arguments than formal ones , Less formal parameters will be nil. This is consistent with the multivariable assignment .
Function can return multiple values :
function func (void)
return 1,2
end
a,b = func()
print(a) --> 1
print(b) --> 2
adopt () You can force a function to return a value :
function func (void)
return 1,2
end
print(func()) --> 1 2
print((func())) --> 1
Lua There can be variable parameters . Use … Three dots indicate , Use a function called arg Of table Said parameters ,arg There is also a domain in n Indicates the number of parameters .
Here is an example :
function func_1(...)
print("The number of args: " .. #{...})
for i,v in ipairs({
...}) do
print(i,v)
end
end
func_1(0, 1, 2, 3)
The result of printing is :
The number of args: 4
1 0
2 1
3 2
4 3
Lua And Nginx
When Nginx When the standard module and configuration cannot flexibly adapt to the system requirements , You can consider using it Lua Expand and customize Nginx service .OpenResty It integrates a large number of sophisticated Lua library 、 Third-party module , It can be easily built to handle ultra-high concurrency 、 Highly scalable Web service , So here we choose OpenResty Provided lua-nginx-module programme .
lua-nginx-module The module is already Lua Provides a wealth of Nginx call API, Every API Each has its own environment , See for a detailed description Nginx API for Lua. Here are only the basic API Use .
First, one Lua Script Services , The configuration file is as follows :
location ~ /lua_api {
# Exemplary Nginx Variable
set $name $host;
default_type "text/html";
# adopt Lua File content processing
content_by_lua_file /home/www/nginx-api.lua;
}
content_by_lua_file : In the specified file <path-to-lua-script-file> contain Lua Code
Nginx Processing sequence stage
(1)post-read
Read request content stage ,nginx After reading and parsing the request header, it starts running immediately ;
(2)server-rewrite
server Request address rewrite phase ;
(3)find-config
Configuration search phase , Used to complete the current request and location Pairing between counterweights ;
(4)rewrite
location Request address rewrite phase , When ngx_rewrite Instructions for location in , It is at this stage that it runs ;
(5)post-rewrite
Request address rewrite submission phase , When nginx complete rewrite Internal jump action required by stage , If rewrite If there is such a requirement in the stage ;
(6)preaccess
Access rights check preparation stage ,ngx_limit_req and ngx_limit_zone Run at this stage ,ngx_limit_req You can control the access frequency of requests ,ngx_limit_zone You can control the concurrency of access ;
(7)access
Permission check stage ,ngx_access Run at this stage , Configuration instructions mostly perform tasks related to access control , Such as checking the access rights of users , Check the source of the user IP Is it legal ;
(8)post-access
Access check submission phase ;
(9)try-files
Configuration item try_files Processing stage ;
(10)content
Content generation stage , It is the most important stage of all request processing stages , Because the instructions at this stage are usually used to generate HTTP Respond to the content of ;
(11)log
Log module processing stage ;
ngx_lua Operation instruction
ngx_lua Belong to nginx Part of , Its execution instructions are contained in nginx Of 11 Step by step , The corresponding processing stage can do plug-in processing , Pluggable architecture , however ngx_lua Not all phases will run ; In addition, the instructions can be in http、server、server if、location、location if Configure several ranges :

边栏推荐
猜你喜欢

Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server

Calling dht11/22 temperature and humidity sensor in Proteus simulation Arduino

我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律

Visual display method of machine learning project

Character stream learning 14.3
软件测试功能测试全套常见面试题【功能测试】面试总结4-2

Redis 哈希Hash底层数据结构

【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析

【软考软件评测师】2014综合知识历年真题

With double-digit growth in revenue and profit, China Resources Yibao has quietly created these new products worth more than 100 million
随机推荐
2022年土木,建筑与环境工程国际会议(ICCAEE 2022)
常用泰勒展开
Zabbix4.0 uses SNMP agent to monitor vcenter6.5
Find and leverage xxE – XML external entity injection
NDK series (6): let's talk about the way and time to register JNI functions
QT with OpenGL(Shadow Mapping)(平行光篇)
Disable caching with meta HTML tags in all browsers
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
JUC工具包学习
C # delegate usage -- console project, which implements events through delegation
Date的使用
实现按照序号命名的txt文件由后往前递补重命名文件
proteus仿真arduino中调用DHT11/22温湿度传感器
华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
Flink怎么使用Savepoint
Implicit indicators for evaluating the advantages and disadvantages of automated testing
XML 外部实体 (XXE) 漏洞及其修复方法
Redis 哈希Hash底层数据结构
Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
Software test function test full set of common interview questions [function test] interview summary 4-2