博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LUA学习笔记(第1-4章)
阅读量:3526 次
发布时间:2019-05-20

本文共 2888 字,大约阅读时间需要 9 分钟。

需要一种简单的脚本语言来代替批处理,它需要足够小巧,同时功能上也应该足够强劲,自然选择了LUA语言。

第一章

Hello World
print(‘Hello World’) print("Hello World")
字符串可以用'string' 或"string",在一个程序中尽量保持一致性。
本机器LUA的运行环境使用的luadist
C:\Windows\System32>luadistLuaDist-git 0.2.3 - Lua package manager for the LuaDist deployment system.Released under the MIT License. See https://github.com/luadist/luadist-gitUsage: luadist [DEPLOYMENT_DIRECTORY]  [ARGUMENTS...] [-VARIABLES...]    Commands:        help      - print this help        install   - install modules        remove    - remove modules        refresh   - update information about modules in repositories        list      - list installed modules        info      - show information about modules        search    - search repositories for modules        fetch     - download modules        make      - manually deploy modules from local paths        upload    - upload installed modules to their repositories        selftest  - run the selftest of LuaDist    To get help on specific command, run:        luadist help C:\Windows\System32>
执行:
C:\Windows\System32>luaLua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio> print("Hello World")Hello World>
LUA中语句之间不需要分隔符
;
退出交互模式:
调用os.exit()或CTRL + C或CTRL + Z输入回车
LUA的参数
C:\Windows\System32>lua --helpusage: lua [options] [script [args]].Available options are:  -e stat  execute string 'stat'  -l name  require library 'name'  -i       enter interactive mode after executing 'script'  -v       show version information  --       stop handling options  -        execute stdin and stop handling optionsC:\Windows\System32>
lua -i test.lua
执行完test.lua直接进入交互模式
交互模式下运行lua文件:
dofile("test.lua")
lua -e "print('Hello World')"
E:\MyDocument\Desktop\coding>lua -e "print('Hello World')"Hello WorldE:\MyDocument\Desktop\coding>
LUA注释
单行:--print("Hello World")
多行:--[[
print("Hello World")
]]
LUA注释技巧:
---[[
print("Hello World")
--]]
这样可以取消注释
交互模式下打印任何表达式的值得另一个方法
=math.pi
LUA遍历参数:
for i=0,#arg do	print(arg[i])end
E:\MyDocument\Desktop\coding>lua test.lua "Hello" 1 2 3 4 5test.luaHello12345E:\MyDocument\Desktop\coding>
arg[0]为文件的文件名

第二章

Lua是一种动态类型的语言。在语言中没有类型定义的语法,每个值都带有其自身的类型信息。
输出类型信息:
print(type("123")) --string
print(type(123))--number
在LUA语言中,nil是一种类型,它只有一个值nil,它的主要功能是用于区别其他任何值。
boolean类型:LUA将nil和false认定为假,其他为真
number类型:双精度实数(double)
string类型:LUA中的字符串是不可变值。字符串连接使用
string1.. string2
使用#string来获得字符串的长度
table类型:
table是LUA中唯一的数据结构
arr = {}--空的table
赋值:arr["key"] = value --key既可以是整数,也可以是字符串或其他类型。
使用table表示一个传统的数组:

第三章

取小数点后两位:
lua -e "print(math.pi - math.pi % 0.01)"--输出3.14
table构造式:
days = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"}
days[1]初始化字符串"Sunday"

第四章

LUA支持多重赋值
x,y = y,x -->交换X,Y的值
在多重赋值中,LUA先对等号右边的所有元素求值,然后才执行赋值。
多重赋值一般用于收集函数的多个返回值。如:a,b = function()
i = 10 --全局变量
local j = 100 --局部变量
数字型for
for var = exp1, exp2, exp3 do
<执行体>
end
var由exp1到exp2步长为exp3
泛型for
for i,v ipairs(arr) do
print(v)
end
你可能感兴趣的文章