转载请标明出处:

http://blog.csdn.net/forezp/article/details/78616622

本文出自方志朋的博客

什么是lua

Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。

Lua 是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发。

—摘抄 http://www.runoob.com/lua/lua-tutorial.html

环境搭建

注意: 在上一篇文章中,OpenResty已经有了Lua的环境,这里安装的是单独的Lua环境,用于学习和开发Lua。大多数的电脑是Windowds版本的电脑,Windows版本下载地址http://luaforge.net/projects/luaforwindows/。

Linux和Mac电脑下载地址:http://luajit.org/download.html,安装命令如下:

  1. wget http://luajit.org/download/LuaJIT-2.1.0-beta1.tar.gz
  2. tar -xvf LuaJIT-2.1.0-beta1.tar.gz
  3. cd LuaJIT-2.1.0-beta1
  4. make
  5. sudo make install

使用IDEA开发的同学,可以通过安装插件的形式来集成Lua的环境,插件名为EmmyLua,安装插件后,在Idea的右侧栏就会出现Lua的图标,点击图标,就会出现运行Lua代码的窗口。建议使用该插件,可以免去安装Lua环境的麻烦。

第一个Lua程序

安装好环境后,我采用EmmyLua插件的形式,对Lua的入门语法进行一个简单的讲解。

打开EmmyLua的终端,在终端上输入:

  1. print("hi you")

按ctrl+enter,终端显示:

hi you

Lua基本数据类型

lua的基本数据类型有nil、string、boolean、number、function类型。

nil 类型

nil类似于Java中的null ,表示空值。变量第一次赋值为nil。

  1. local num
  2. print(num)
  3. num=100
  4. print(num)

终端输出:

nil

100

number (数字)

Number 类型用于表示实数,和 Java里面的 double 类型很类似。可以使用数学函数

math.floor(向下取整) 和 math.ceil(向上取整) 进行取整操作。


  1. local order = 3.99
  2. local score = 98.01
  3. print(math.floor(order))
  4. print(math.ceil(score))

输出:

3

99

string 字符串

Lua 中有三种方式表示字符串:

1、使用一对匹配的单引号。例:‘hello’。

2、使用一对匹配的双引号。例:"abclua

3.字符串还可以用一种长括号(即[[ ]]) 括起来的方式定义

  1. ocal str1 = 'hello world'
  2. local str2 = "hello lua"
  3. local str3 = [["add\name",'hello']]
  4. local str4 = [=[string have a [[]].]=]
  5. print(str1) -->output:hello world
  6. print(str2) -->output:hello lua
  7. print(str3) -->output:"add\name",'hello'
  8. print(str4) --

table (表)

Table 类型实现了一种抽象的“关联数组”。“关联数组”是一种具有特殊索引方式的数组,索引通常是字符串(string) 或者 number 类型,但也可以是除 nil 以外的任意类型的值。

  1. local corp = {
  2. web = "www.google.com", --索引为字符串,key = "web",
  3. -- value = "www.google.com"
  4. telephone = "12345678", --索引为字符串
  5. staff = {"Jack", "Scott", "Gary"}, --索引为字符串,值也是一个表
  6. 100876, --相当于 [1] = 100876,此时索引为数字
  7. -- key = 1, value = 100876
  8. 100191, --相当于 [2] = 100191,此时索引为数字
  9. [10] = 360, --直接把数字索引给出
  10. ["city"] = "Beijing" --索引为字符串
  11. }
  12. print(corp.web) -->output:www.google.com
  13. print(corp["telephone"]) -->output:12345678
  14. print(corp[2]) -->output:100191
  15. print(corp["city"]) -->output:"Beijing"
  16. print(corp.staff[1]) -->output:Jack
  17. print(corp[10]) -->output:36

function(函数)

在 Lua 中,函数 也是一种数据类型,函数可以存储在变量中,可以通过参数传递给其他函

数,还可以作为其他函数的返回值。


  1. local function foo()
  2. print("in the function")
  3. --dosomething()
  4. local x = 10
  5. local y = 20
  6. return x + y
  7. end
  8. local a = foo --把函数赋给变量
  9. print(a())
  10. --output:
  11. in the function
  12. 30

表达式

~= 不等于

逻辑运算符 说明
and 逻辑与
or 逻辑或
not 逻辑非
  • a and b 如果 a 为 nil,则返回 a,否则返回 b;
  • a or b 如果 a 为 nil,则返回 b,否则返回 a。
  1. local c = nil
  2. local d = 0
  3. local e = 100
  4. print(c and d) -->打印 nil
  5. print(c and e) -->打印 nil
  6. print(d and e) -->打印 100
  7. print(c or d) -->打印 0
  8. print(c or e) -->打印 100
  9. print(not c) -->打印 true
  10. print(not d) --> 打印 false

在 Lua 中连接两个字符串,可以使用操作符“…”(两个点).

  1. print("Hello " .. "World") -->打印 Hello World
  2. print(0 .. 1) -->打印 01

控制语句

单个 if 分支 型

  1. x = 10
  2. if x > 0 then
  3. print("x is a positive number")
  4. end

两个分支 if-else 型

  1. x = 10
  2. if x > 0 then
  3. print("x is a positive number")
  4. else
  5. print("x is a non-positive number")
  6. end

多个分支 if-elseif-else 型:

  1. score = 90
  2. if score == 100 then
  3. print("Very good!Your score is 100")
  4. elseif score >= 60 then
  5. print("Congratulations, you have passed it,your score greater or equal to 60")
  6. --此处可以添加多个elseif
  7. else
  8. print("Sorry, you do not pass the exam! ")
  9. end

for 控制结构

Lua 提供了一组传统的、小巧的控制结构,包括用于条件判断的 if 用于迭代的 while、repeat

和 for,本章节主要介绍 for 的使用.

for 数字型

for 语句有两种形式:数字 for(numeric for) 和范型 for(generic for) 。

数字型 for 的语法如下:

  1. for var = begin, finish, step do
  2. --body
  3. end

实例1:

  1. for i = 1, 5 do
  2. print(i)
  3. end
  4. -- output:
  5. 1 2 3 4 5

实例2:

  1. for i = 1, 10, 2 do
  2. print(i)
  3. end
  4. -- output:
  5. 1 3 5 7 9

for 泛型

泛型 for 循环通过一个迭代器(iterator) 函数来遍历所有值:

  1. -- 打印数组a的所有值
  2. local a = {"a", "b", "c", "d"}
  3. for i, v in ipairs(a) do
  4. print("index:", i, " value:", v)
  5. end
  6. -- output:
  7. index: 1 value: a
  8. index: 2 value: b
  9. index: 3 value: c
  10. index: 4 value: d

lua的入门就到这里,因为lua语法虽少,但细节有很多,不可能花很多时间去研究这个。入个门,遇到问题再去查资料就行了。另外需要说明的是本文大部分内容为复制粘贴于OPenResty 最佳实践,感谢原作者的开源电子书,让我获益匪浅。更多内容请参考:

lua入门教程:http://www.runoob.com/lua/lua-tutorial.html

OPenResty 最佳实践: https://moonbingbing.gitbooks.io/openresty-best-practices/content/index.html




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

Openresty最佳案例 | 第2篇:Lua入门的更多相关文章

  1. Openresty最佳案例 | 第4篇:OpenResty常见的api

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616660 本文出自方志朋的博客 获取请求参数 vim /usr/example/exa ...

  2. Openresty最佳案例 | 第8篇:RBAC介绍、sql和redis模块工具类

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616738 本文出自方志朋的博客 RBAC介绍 RBAC(Role-Based Acce ...

  3. Openresty最佳案例 | 第9篇:Openresty实现的网关权限控制

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616779 本文出自方志朋的博客 简介 采用openresty 开发出的api网关有很多 ...

  4. Openresty最佳案例 | 第7篇: 模块开发、OpenResty连接Redis

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616714 本文出自方志朋的博客 Lua模块开发 在实际的开发过程中,不可能把所有的lu ...

  5. Openresty最佳案例 | 第6篇:OpenResty连接Mysql

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616698 本文出自方志朋的博客 centos 安装mysl Centos系统下安装my ...

  6. Openresty最佳案例 | 第5篇:http和C_json模块

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616672 本文出自方志朋的博客 http客户端 Openresty没有提供默认的Htt ...

  7. Openresty最佳案例 | 第3篇:Openresty的安装

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616645 本文出自方志朋的博客 我的服务器为一台全新的centos 7的服务器,所以从 ...

  8. Openresty最佳案例 | 第1篇:Nginx介绍

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616591 本文出自方志朋的博客 Nginx 简介 Nginx是一个高性能的Web 服务 ...

  9. Openresty最佳案例 | 汇总

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616856 本文出自方志朋的博客 目录 Openresty最佳案例 | 第1篇:Ngin ...

随机推荐

  1. Linux Kernel文件系统写I/O流程代码分析(一)

    Linux Kernel文件系统写I/O流程代码分析(一) 在Linux VFS机制简析(二)这篇博客上介绍了struct address_space_operations里底层文件系统需要实现的操作 ...

  2. js录制视频并保存

    使用webAPI录制视频 经测试, 只在谷歌和火狐浏览器里起效. 代码: const streamVideo = document.querySelector('.stream') const pla ...

  3. asp ajax

    //[AjaxPro.AjaxMethod()] //public DataTable loadChecked() //{ // return BDAContext.GetObject<ICNP ...

  4. 使用Access作数据库

    import java.sql.*; public class ConnectAccess { public static void main(String args[]){ ConnectAcces ...

  5. lua_nginx_module用例

    content_by_lua server { listen ; server_name lua.luckybing.top; location / { default_type 'text/plai ...

  6. Thrift笔记(五)--Thrift server源码分析

    从(四)server代码跟进 public static void simple(MultiplicationService.Processor processor) { try { TServerT ...

  7. js之闭包

    函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个对Array的求和.通常情况下,求和的函数是这样定义的: function sum(arr) { ret ...

  8. re模块——正则表达式操作

    一.什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 r ...

  9. CentOS安装chrome-浏览器

    首先,在CentOS中安装软件,通过yum命令来安装软件,就要在系统中的软件源中添加软件源节点,即在目录./etc/yum.repos.d/下的CentOS-Base.repo文件中做修改:在控制台C ...

  10. Linux中怎么从root用户切换到普通用户

    su是在用户间切换,可以是从普通用户切换到root用户, test@ubuntu:~$ su Password:  root@ubuntu:/home/test# 也可以是从root用户切换到普通用户 ...