执行下面的脚本用luajit test.lua即可

一、变量及逻辑运算

  1. --number, string, boolean, table, function, thread, userdata, nil
  2.  
  3. --<1>Number demo
  4. x = 11 --It's number type,but not an integer
  5. --The two common variable name below in the lua language is recommended.
  6. exampleVar = 10e5 --equal 1000000
  7. example_var = 666
  8. exampleHexadecimal = 0xFAE --equal 4014
  9.  
  10. --<2>String demo
  11. exampleStrings = "Hello 123 &^*& \n"
  12.  
  13. --nil
  14. print(notDefineVariableIsNil) -- nil
  15.  
  16. -- MathOperator demo
  17. -- (),a ^b, not a, #a, -a, a*b, a/b, a%b, a+b, a-b, a..b, a<b, a>b(return boolean value), a ~= b(tilde equals !), a == b
  18. -- a and b, a or b
  19. exampleMathModuloOperator = 10/3 --Supporting + - * / %, and so forth.
  20. exampleDecimalRemainder = 10 % 3.3 -- equal 0.1
  21. exponentNum1 = 2
  22. exponentPower = 4
  23. exampleExponentCal = exponentNum1 ^ exponentPower -- 16
  24. exampleBoolean = true
  25. print(not exampleBoolean) -- false
  26. print(-exponentNum1) -- -2
  27. print( (exponentNum1 + exponentPower) * 4) -- 24
  28.  
  29. --逻辑运算符
  30. exponentNum1 = 10
  31. exponentPower = 4
  32.  
  33. print(exponentNum1 and exponentPower) -- 4,因为第一个参数为true,所以返回4
  34. print(exponentNum1 or exponentPower) --10,因为第一个参数为true
  35.  
  36. exponentNum1 = false
  37. print(exponentNum1 and exponentPower) --false,因为第一个参数为false,所以返回false
  38.  
  39. a = 4
  40. b = 10
  41.  
  42. print( a < b or "this statement is false") --true,or的话第一个参数为true直接返回
  43. print( a > b or "this statement is false") --this statement is false,or的话第一个参数为false返回第二个参数
  44. print( not(a + 11 < b) or "this statement is false") --true,or的话第一个参数为true直接返回
  45.  
  46. -- Catenation string
  47. exampleCatenationString = "Hello " .. "World!" -- Hello World!
  48. print(#exampleCatenationString) -- 12
  49.  
  50. --print(exampleCatenationString)

二、条件语句

  1. --条件运算
  2. if 3 > 5 then
  3. print("this statement is true")
  4. elseif 20 > 15 then
  5. print("20 > 15")
  6. else
  7. print("this statement is false")
  8. end
  9.  
  10. --常用函数
  11. print(type(20)) --number,类型为number类型
  12.  
  13. testVar = nil
  14. if type(testVar) == "number" then
  15. print("testVar is a number")
  16. elseif type(testVar) == "string" then
  17. print("testVar is a string")
  18. else
  19. print(testVar)
  20. print("testVar is a else")
  21. end

三、循环

  1. print("------while loop example------")
  2. iter = 0
  3. while iter <= 10 do
  4. print(iter)
  5. iter = iter + 1
  6. end
  7.  
  8. print("------for loop example------")
  9. --等同于while循环,默认递增为1,所以不用写
  10. for i = 0, 10 do
  11. print(i)
  12. end
  13.  
  14. print("-----改变递增系数-----")
  15. --以3为递增
  16. for i = 0, 10, 3 do
  17. print(i)
  18. end
  19.  
  20. print("-----增加退出循环条件-----")
  21. for i = 0, 10 do
  22. print(i)
  23. if i == 5 then break end --假如比较简单,可以直接一行
  24. end
  25.  
  26. print("------until loop example------")
  27. element = 0
  28. repeat
  29. print(element)
  30. element = element + 1
  31. until element > 10

四、基本表

  1. t = {1, "hello", true, four = 4, five = true, six = "world"} --可以放置任何类型
  2. print(t[2]) --hello
  3. print(t[4]) --nil,直接打印第4个元素为nil
  4. --以键值对的方式访问
  5. print(t["four"]) --4
  6. print(t.four) --4
  7. print(t.five, t["six"]) --true world
  8.  
  9. --sizeof
  10. print(#t) --3,nil的不算在内
  11.  
  12. examT = {1, 2, 3, 4, 5, 6}
  13. print(#examT) --6
  14.  
  15. examString = "know"
  16. print(#examString) --4

五、函数

(1)命名函数的两种方式.

  1. function f()
  2. print("Hello")
  3. end
  4.  
  5. g = function()
  6. print("Hello again")
  7. end
  8.  
  9. f() --推荐这种用法
  10. g()
  11.  
  12. function println(value)
  13. print(value)
  14. end
  15. println("Arun")
  16. println(10)
  17.  
  18. y = 911 --全局变量
  19. function addPrint(a, b)
  20. x = "101010101010"
  21. print(x)
  22. y = 912
  23. return a + b
  24. end
  25.  
  26. t = {addPrint(1, 2), 23}
  27. println(t[1])
  28. print(x)
  29. print(y)

(2)变量的作用域

  1. --x = 12 --打开这个变量,可以看出x变量的值的范围
  2. function add(a, b)
  3. x = 10 --本地变量
  4. local y = 11
  5. print(x) --10
  6. return a + b
  7. end
  8.  
  9. print(x) --nil
  10.  
  11. print(add(5,15)) --20
  12. print(x) --10,函数内部加local关键字,它会变成全局变量
  13. print(y) --nil

六、内存管理

注意内存的传值引用和传址引用

  1. ------------------
  2. x = 10
  3. y = x
  4. x =20
  5. print(x)
  6. print(y)
  7.  
  8. --[[输出=>
  9. 20
  10. 10]]
  11. ------------------
  12. m = {10, 20, 30}
  13. n = m
  14. m[2] = 40
  15.  
  16. print(m[2])
  17. print(n[2])
  18. --[[输出=>
  19. 40
  20. 40]]

七、闭包

  1. function f()
  2. local x = 1
  3. return function() print(x); end
  4. end
  5.  
  6. printHello = f()
  7. printHello() --1
  8. -------------------------------
  9. function createIter()
  10. local i = 0
  11. return function() print(i); i = i + 1 end
  12. end
  13.  
  14. iter = createIter()
  15. iter() --0
  16. iter() --1
  17. iter() --1

八、table面向对象实现

  1. Player = {
  2. x = 0, y = 0,
  3. name = "",
  4. new = function()
  5. p = {}
  6. for k, v in pairs(Player) do
  7. p[k] = v
  8. end
  9. return p
  10. end,
  11.  
  12. move = function(obj, x,y)
  13. obj.x = obj.x +x
  14. obj.y = obj.y +y
  15. end
  16. }
  17.  
  18. p1 = Player.new()
  19.  
  20. p1.x = 10
  21. p1.y = 20
  22. p1.name = "Bob"
  23.  
  24. p2 = Player.new()
  25. p2.x = 30
  26. p2.y = 50
  27. p2.name = "Steve"
  28.  
  29. print(p1.x, p1.y, p1.name) --10 20 Bob
  30. print(p2.x, p2.y, p2.name) --30 50 Steve
  31.  
  32. p1.move(p1, 10, 10)
  33. p2.move(p2,70,90)
  34. print(p1.x, p1.y) --20 30
  35. print( p2.x, p2.y) --100 140

九、meta表高级用法,重写原始操作符

  1. Vector2 = {
  2. x = 0, y =0,
  3. mt = {},
  4. New = function()
  5. local vec = {}
  6.  
  7. setmetatable(vec, Vector2.mt)
  8.  
  9. vec.x = Vector2.x
  10. vec.y = Vector2.y
  11. vec.mt = Vector2.mt
  12. vec.Translate = Vector2.Translate
  13. return vec
  14. end,
  15. Translate = function(self, dx, dy)
  16. self.x = self.x + dx
  17. self.y = self.y + dy
  18. end
  19. }
  20. Vector2.mt.__add = function(v1, v2)
  21. local vec = Vector2.New()
  22. vec.x = v1.x + v2.x
  23. vec.y = v1.y + v2.y
  24. return vec
  25. end
  26.  
  27. Vector2.mt.__sub = function(v1, v2)
  28. local vec = Vector2.New()
  29.  
  30. vec.x = v1.x - v2.x
  31. vec.y = v1.y - v2.y
  32.  
  33. return vec
  34. end
  35.  
  36. -- __mul, __div, __mod,
  37. Vector2.mt.__eq = function(v1, v2)
  38. return v1.x == v2.x and v1.y == v2.y
  39. end
  40. -- __lt, __le, __gt, __ge
  41.  
  42. Vector2.mt.__tostring = function(vec)
  43. return "(" .. vec.x .. ", " .. vec.y .. ")"
  44. end
  45.  
  46. Vector2.mt.__metatable = "Private"
  47.  
  48. --[[Vector2.mt.__index = Vector2
  49.  
  50. Vector2.mt.__newindex = function(t, k, v)
  51. error("Cannot change values of Vector2 instance.")
  52. end]]
  53.  
  54. v1 = Vector2.New()
  55. v1.x = 10
  56. v1.y = 20
  57. --v1.Translate(v1, 10, 10)
  58. --等同于
  59. --v1:Translate(10, 10)
  60. --print(v1.x, v1.y) --20 30
  61.  
  62. v2 = Vector2.New()
  63. v2.x = 30
  64. v2.y = 40
  65.  
  66. v3 = v1 + v2
  67. print(v3.x, v3.y) --40 60
  68.  
  69. v4 = v1 - v2
  70. print(v4.x, v4.y) -- -20 -20
  71.  
  72. print(v4) -- (-20, -20)
  73. print(getmetatable(v4)) -- Private
  74. print(v1 == v2) -- false
  75.  
  76. --setmetatable(v4, nil) --test.lua:71: cannot change a protected metatable

十、高级循环

  1. t1 = {1, 2, 3}
  2. t1[2] = nil
  3. for i=1, #t1 do
  4. print(i, t1[i])
  5. end
  6. --[[1 1
  7. 2 2
  8. 3 3]]
  9.  
  10. t2 = {one = 1, two = 2, three = 3}
  11.  
  12. for k,v in pairs(t2) do --是pairs而不是ipairs
  13. print(k, t2[k])
  14. end
  15. --[[
  16. one 1
  17. three 3
  18. two 2]]
  19.  
  20. t3 = {1, 2, 3, 4, 5}
  21. function numIter(tb, start)
  22. i = start
  23. return function()
  24. i = i + 1
  25. if tb[i - 1] then
  26. return i - 1, tb[i - 1]
  27. else
  28. return nil
  29. end
  30. end
  31.  
  32. end
  33.  
  34. for k,v in numIter(t3, 1) do
  35. print(k, v)
  36. end
  37. --[[
  38. 1 1
  39. 2 2
  40. 3 3
  41. 4 4
  42. 5 5]]

十一、lua内运行外部代码的三种方式

(1)test1.lua

  1. for i = 0, 10 do
  2. print("hello")
  3. end
  4. return 100

(2)main.lua

  1. -- dofile, loadfile load
  2. --<1>加载执行文件第一种方式
  3. --dofile("/Users/00arunalldata00/006_eleallproject/002ngconf/002_camel-agent-deploy/router-lua-module/aruntest/lua_learning/test1.lua")
  4.  
  5. --<2>加载执行文件第二种方式
  6. --[[function newDoFile(filename)
  7. f = assert(loadfile(filename))
  8. return f()
  9. end
  10.  
  11. newDoFile("./test1.lua")
  12. print(newDoFile("./test1.lua")) -- 100]]
  13.  
  14. --<3>加载执行文件第三种方式
  15. --(1)load和函数调用第一种区别
  16. f = load("print(20)")
  17. f() -- 20
  18.  
  19. function g()
  20. print(20)
  21. end
  22. g() -- 20
  23. --(2)load和函数调用第二种区别
  24. x = 10
  25. local x = 20
  26. function f1()
  27. x = x + 1
  28. print(x)
  29. end
  30. f1() -- 21
  31.  
  32. g1 = load("x = x + 1;print(x)")
  33. g1() -- 11

十二、

 

Reference:

https://www.youtube.com/playlist?list=PL0o3fqwR2CsWg_ockSMN6FActmMOJ70t_

035_lua快速入门的更多相关文章

  1. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  2. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  3. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  4. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  5. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. Mybatis框架 的快速入门

    MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...

  7. grunt快速入门

    快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...

  8. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  9. 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. Hadoop — Yarn原理解析

    1. 概述 Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台:而MapReduce等运算程序则相当运行于操作系统之上的应用程序. 2. YARN的重要概念 ...

  2. java 写一个 map reduce 矩阵相乘的案例

    1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...

  3. IO流--字符流与字节流--File类常用功能

    IO流的常用方法: 1: 文件的读取和写入图解: 2:字节流: 读写文件的方法: 一般效率读取: 读取文件:        FileInputStream(); 写数据:            Fil ...

  4. 如何更改vs2013中git的远程仓库url地址

    可以通过修改Git库配置文件实现,请看下图:

  5. Elasticsearch入门实践

    官网:https://www.elastic.co/ 下载:https://www.elastic.co/downloads/elasticsearch 文档:https://www.elastic. ...

  6. spring boot零碎知识点待补充

    @Controller 和@RestController的区别 @RestController相当于同时使用了@Controller和@ResponseBody  即不会使用视图解析器,返回值直接返回 ...

  7. 华为平板安装APK,提示“该安装包未包含任何证书”

    有的平板上会有错误现象 打包时签名勾选v1即可.

  8. vivalidi 一款由Web技术诞生的Web浏览器

    vivalidi https://vivaldi.com/ A million ways to customize everything The world is a colorful place b ...

  9. Javaweb学习笔记——(十)——————response对象,response字符流缓冲器,响应头,状态码,重定向,requset对象,路径和乱码

    请求响应对象: request和response *当服务器接收都请求后,服务器会创建request和response对象,把请求数据封装到request对象中: *然后调用Servlet的sevic ...

  10. Creating A Moddable Unity Game

    前言: 对游戏进行修改与拓展(MOD)是我一直以来感兴趣的东西,我的程序生涯,也是因为在初中接触到GBA口袋妖怪改版开始的,改过也研究过一些游戏的MOD实现方式,早就想在自己的游戏中实现“MOD系统” ...