当前位置:网站首页>Lua语法讲解
Lua语法讲解
2022-06-26 03:54:00 【ha_lydms】
文章目录
一、简介
Lua 是⼀个⼩巧的脚本语⾔。它是巴⻄⾥约热内卢天主教⼤学(Pontifical Catholic University of Rio de Janeiro)⾥的⼀个由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo三⼈所组成的研究⼩组于1993年开发的。 其设计⽬的是为了通过灵活嵌⼊应⽤程序中从⽽为应⽤程序提供灵活的扩展和定制功能。Lua由标准C编写⽽成,⼏乎在所有操作系统和平台上都可以编译,运⾏。Lua并没有提供强⼤的库,这是由它的定位决定的。所以Lua不适合作为开发独⽴应⽤程序的语⾔。Lua 有⼀个同时进⾏的JIT项⽬,提供在特定平台上的即时编译功能。
简单来说:
Lua 是⼀种轻量⼩巧的脚本语⾔,⽤标准C语⾔编写并以源代码形式开放, 其设计⽬的是为了嵌⼊应⽤程序中,从⽽为应⽤程序提供灵活的扩展和定制功能。
lua语⾔具有以下特性
- ⽀持⾯向过程(procedure-oriented)编程和函数式编程(functional programming);
- ⾃动内存管理;只提供了⼀种通⽤类型的表(table),⽤它可以实现数组,哈希表,集合,对象;
- 语⾔内置模式匹配;闭包(closure);函数也可以看做⼀个值;提供多线程(协同进程,并⾮操作系统所⽀持的线程)⽀持;
- 通过闭包和table可以很⽅便地⽀持⾯向对象编程所需要的⼀些关键机制,⽐如数据抽象,虚函数,继承和重载等。
二、lua安装
yum install -y gcc
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel
curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar -zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make linux test
make install
三、快速入门
创建hello.lua文件
touch hello.lua
在文件中添加
print("hello");
执行lua命令
lua hello.lua
四、基本语法
- lua有交互式编程和脚本式编程。
- 交互式编程就是直接输⼊语法,就能执⾏。
- 脚本式编程需要编写脚本,然后再执⾏命令 执⾏脚本才可以。⼀般采⽤脚本式编程。(例如:编写⼀个hello.lua的⽂件,输⼊⽂件内容,并执⾏lua hell.lua即可)
1、注释
⼀⾏注释:两个减号是单⾏注释:
--
多行注释:
--[[
多行注释
多行注释
--]]
2、关键字
| and | break | do | else |
|---|---|---|---|
| elseif | end | false | for |
| function | if | in | local |
| nil | not | or | repeat |
| return | then | true | until |
| while |
3、变量
全局变量/局部变量。默认定义的变量就是全局变量。如果要使用局部变量,需要local来定义。
如果变量没有初始化:则它的值为nil。Java 中null不同。
-- 全局变量赋值
a = 1
-- 局部变量
local b = 2
4、Lua中的数据类型
Lua是动态类型语言,变量不需要类型定义,只需要为变量赋值。值可以存储在变量中,作为参数传递或结果返回。
Lua中有8种基本类型,分别为:nil、boolean、number、string、userdata、function、thread、table。
| 数据类型 | 描述 |
|---|---|
| nil | 这个最简单,只有值nil属于该类,表示⼀个⽆效值(在条件表达式中相当于false)。 |
| boolean | 包含两个值:false和true。 |
| number | 表示双精度类型的实浮点数 |
| string | 字符串由⼀对双引号或单引号来表示 |
| function | 由 C 或 Lua 编写的函数 |
| userdata | 表示任意存储在变量中的C数据结构 |
| thread | 表示执⾏的独⽴线路,⽤于执⾏协同程序 |
| table | Lua 中的表(table)其实是⼀个"关联数组"(associative arrays),数组的索引可以是数字、字符串或表类型。在 Lua ⾥,table 的创建是通过"构造表达式"来完成,最简单构造表达式是{},⽤来创建⼀个空表。 |
5、流程控制
类似if、else
-- [0 为 true]
if (0) then
print("0 为 true")
else
print("0 不为 true")
end
6、函数控制
lua也可以定义函数,类似于java中方法
--[[ 函数返回两个值的最大值 ]]
function max(num1 , num2)
if(num1 > num2) then
result = num1;
else
result = num2;
end
return result;
end
-- 调用函数
print("两值比较最大值为 ",max(10,4))
print("两值比较最大值为 ",max(5,6))
7、require 引用函数
require用于引入其它的模块。
8、定义数组
local props = {
host = "192.168.200.128",
port = 3306,
database = "root",
password = "123456"
}
9、Nginx中配置执行lua
指定某页面,去执行lua程序
location /ad_update {
content_by_lua_file /root/lua/ad_update.lua;
}
10、其它语法
-- 关闭数据库连接(调用方法)
db:close
-- 定义全局变量
ngx.header.content_type="application/json;charset=utf-8"
-- 定义局部变量
local cjson = require("cjson")
-- 定义局部数据
local position = uri_args["position"]
-- 引入包
local redis = require("resty.redis")
-- 连接Redis
red:connect(ip,port)
边栏推荐
- Custom parameter QR code picture combined with background picture to generate new picture node environment
- 軟件調試測試的十大重要基本准則
- JS to achieve the effect of text marquee
- 816. 模糊坐标
- Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.
- 动态线段树leetcode.715
- Three level menu applet
- 第 4 篇:绘制四边形
- Dix critères de base importants pour les essais de débogage de logiciels
- Open source! Vitae model brushes the world's first again: the new coco human posture estimation model achieves the highest accuracy of 81.1ap
猜你喜欢

开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP

评价——层次分析

Uni app, the text implementation expands and retracts the full text

Camera-memory内存泄漏分析(二)

What's wrong with connecting MySQL database with eclipse and then the words in the figure appear

ABP framework

Optimization - multi objective planning

TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor

Detr3d multi 2D picture 3D detection framework

Slide the menu of uni app custom components left and right and click switch to select and display in the middle
随机推荐
【掘金运营套路揭露】真心被掘金的套路....
评价——层次分析
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
763. 划分字母区间
asp.net网页选择身份进行登录的简单代码,asp连接数据库,使用asp:Panel、asp:DropDownList控件
C # knowledge structure
Webrtc series - 6-connections tailoring for network transmission
Uni app custom drop-down selection list
1. foundation closing
ipvs之ipvs0网卡
力扣79单词搜索
Nepal graph learning Chapter 3_ Multithreading completes 6000w+ relational data migration
Slide the menu of uni app custom components left and right and click switch to select and display in the middle
Analysis of camera memory memory leakage (II)
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
Uni app custom navigation bar component
js实现文字跑马灯效果
2022.6.23-----leetcode. thirty
[collection of good books] from technology to products
2022.6.25-----leetcode. Sword finger offer 091