当前位置:网站首页>The basic knowledge of scripting language Lua summary
The basic knowledge of scripting language Lua summary
2022-08-01 13:03:00 【dvlinker】
目录
Lua Language is a team of the Catholic university in Rio DE janeiro, Brazil to 1993 年开发的,是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开发,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能.Many projects are used toLua脚本,今天我们就来介绍一下Lua的一些基础知识.

1、Lua环境安装
1.1、Windows版本安装
Can go to download to this addressWindows版本的安装包:https://github.com/rjpcomputing/luaforwindows/releases,选择合适的windows版本安装即可.Double click on the install will be installed afterSciTE工具:

In the tool can writeluaScript and running script:

1.2、Linux版本的安装
下载源码包: curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz
解压:tar zxf lua-5.3.0.tar.gz
安装readline库(若未安装) :yum -y install readline-devel ncurses-devel
进入解压目录:cd lua-5.3.0
编译:make linux
安装:make install
2、Lua基础知识
2.1、基本语法
1)Lua支持交互式编程,At the command line input program and immediately see the effect;Also support script programming,保存为后缀名为“.lua”的文件,执行脚本:lua xxx.lua,Or in the file header plus#!/opt/lua-5.3.0/lua,直接执行:./xxx.lua.
2)LuaBehind each statement don't need to add a semicolon“;”.
3)Single-line comments using two minus sign:--,多行注释使用:--[[ 多行注释]]--.
4)Lua是区分大小写的,Under the general line start connecting the name of a string of capital letters(如 _VERSION)被保留用于 Lua 内部全局变量.
2.2、数据类型
1)Lua中有8种基本类型:nil(空)、boolean、number(数字)、string、userdata(自定义类型)、function(函数)、thread和table(表),可以用type()函数来检查.
2)nil类型:表示一个无效值,它只有一个值nil.
3)boolean类型:Lua把false和nil看作是“假”,其他的都为“真”(eg:0、Empty string, etc“真”).
4)string类型:With single or double quotation marks to indicate,也可以用2个方括号“[[ ]]”表示“一块”字符串.
可以通过“..”As a string connection,使用“#”来计算字符串的长度.
5)userdata类型:Is a user-defined data,Used to represent the application orCA new type of language library created,如:Struct和指针等.
6)function类型:函数也是一种类型,Is all the function,它本身就是一个变量.
7)thread线程类型:表示执行的独立线路,用于执行协同程序.
8)tablbe类型:可看作“关联数组”,数组的索引可以是数字或者是字符串;不同于其他语言的数组把 0 作为数组的初始索引,在 Lua 里表的默认初始索引一般以 1 开始;且table不会固定长度大小,There are new data add its length automatically when growth;没初始化的 table 都是 nil.
2.3、变量
1)Lua是动态类型语言,变量不需要声明,Don't have to type definition,Assignment to a variable that is created the variable,Access to an uninitialized variable also won't go wrong,Just get the results as:nil.
2)LuaThe variables in the unless with local 显式声明为局部变量,Otherwise all global variables,Even if the variables in blocks or function.
3)Lua可以对多个变量同时赋值,变量列表和值列表的各个元素用逗号分开,赋值语句右边的值会依次赋给左边的变量,如:a,b,c = 1,2,3.
4)当变量个数和值的个数不一致时,Lua会一直以变量个数为基础采取以下策略:
a)变量个数 > 值的个数 ,按变量个数补足nil
b)变量个数 < 值的个数,多余的值会被忽略
2.4、运算符
1)算术运算符:+、-、*、/、%(取余)、^(乘幂)、-(负号).
2)关系运算符:==、~=(不等于)、<=、>=、<、>.
3)逻辑运算符:and、or、not(非).
4)连接运算符:..(连接两个字符串).如:“hello”..“Lua” “helloLua”
5)一元运算符:#(Calculating table or the length of the string).
6)运算符优先级:^、not、-(负号)、* /、+ -、..、< > <= >= ~= ==、and、or.
2.5、循环语句
1)While循环
while conditions do statements end其中:用do 和 end The part of enclosed is block,类似于C++中用"{"和"}"括起来的部分.
2)For循环
a)数值for循环for var=exp1 ,exp2 ,exp3 do loop-part endfor将用exp3As a step value fromexp1(初始值)到exp2(终止值),执行loop-part,The step value can be omitted,默认值为1.
b)范型for循环for var1; var2 ... varN inTable or enumeration functiondo loop-part end注意项:控制变量是局部变量,且不能修改其值;可以用breakTo end a cycle.
3)repeat…untilrepeat statements until conditions循环语句可以嵌套.
2.6、条件语句
if conditions then
part1
elseif conditions then
part2
else
partsend1、Lua使用if..else执行条件控制语句
2、Behind each statement to add“then”;
3、Conditional statements can also be nested,不支持switch语句.
2.7、函数
函数的格式如下:
funetion function_name ( arg1 ,arg2... , argn )
function_body
return result_paramsend1)函数代码块以function关键字开头,后跟函数名、参数,以end关键字结尾.
2)LuaIn addition to using keyword in front of the functionlocalAs the local function,Other are global function.
3)result_params:函数返回值,LuaSupport function returns multiple results value,每个值以逗号隔开.
4)LuaThe function can be passed as a parameter to a function.
5)Lua 函数可以接受可变数目的参数,和 C 语言类似,在函数参数列表中使用三点 ... 表示函数有可变的参数.
2.8、模块
1)Lua模块类似于一个封装库,Put some common code in a file,以API接口的形式在其他地方调用.Module is the variable、函数等已知元素组成的table.
2)可以通过require函数来加载模块:require("<模块名>").
2.9、文件操作
1)Lua I/O库用于读取和处理文件,分为简单模式、完全模式.
2)简单模式:拥有一个当前输入文件和一个当前输出文件,And for these files can be read and write related operations.--打开文件,filename为文件名,mode是打开文件的方式 file = io.open (filenane , mode) --设置默认输入文件 io.input(file) --设置默认输出文件 io.output(file) --读取文件内容,paramIs read mode io.read(param) --The last line in the file write content io.write ( "--Test--") --关闭文件 io.close(file)3)完全模式:使用外部的文件句柄来实现.Usually used when needed to handle multiple files at the same time.
使用file:function_name代替io.function_name.--打开文件 file = io.open (filename , mode) --读取文件内容 file : read(param) --The last line in the file write content file :write("--testr--") --关闭文件 file : close (file)/
3、最后
LuaThe addition of scripts,In many applications can reduce the cost and can function reuse,为应用程序提供灵活的扩展和定制功能.It is also a lightweight scripting language,Worthy of further study and application of.
边栏推荐
猜你喜欢
随机推荐
Aeraki Mesh became CNCF sandbox project
CAN通信的数据帧和远程帧
Alibaba Cloud Official Redis Development Specification
R language ggplot2 visualization: use the ggdensity function of the ggpubr package to visualize density plots, use the stat_central_tendency function to add mean vertical lines to the density and cust
一文带你读懂云原生、微服务与高可用
AI目标分割能力,无需绿幕即可实现快速视频抠图
将同级数据处理成树形数据
2022 Go生态圈 rpc 框架 Benchmark
PanGu-Coder:函数级的代码生成模型
MarkDown公式指导手册
那些利用假期学习的职场人,后来都怎么样了?
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
pandas connects to the oracle database and pulls the data in the table into the dataframe, filters all the data from the current time (sysdate) to one hour ago (filters the range data of one hour)
故障007:dexp导数莫名中断
四足机器人软件架构现状分析
【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)
LeetCode_位运算_简单_405.数字转换为十六进制数
Windows 安装PostgreSQL
What is MNIST (what does plist mean)









