Lua学习之Lua标准库

1Lua标准库之数学库(math)

1.1 数学库概念

数学库由算术函数的标准集合组成:如三角函数库(sin,cos,tan,asin,acos,etc),幂指函数(exp,log,log10),舍入函数(floor,ceil)、max、min、pi.数据库也定义了一个幂操作符(^).
在这里插入图片描述

1.2 math.random

产生伪随机数,有以下3中调用方式
1.不带参数,将产生【0,1)范围内的随机数

print(math.random())         --输出结果:0.84018771676347

2.带一个参数n,将产生1到n内的随机数(包含1和n)

print(math.random(20))       --输出结果:17

3.带两个参数m和n,将产生m到n内的随机数(包含m和n)

print(math.random(2,60))     --输出结果:51

1.3 math.randomseed

在程序开始时,使用固定的种子初始化随机数发生器,使得每次运行都会拥有相同的关卡
解决方法:使用系统时间作为种子,通常放在脚本最开始的位置

math.randomseed(os.time())

例:

math.randomseed(os.time())
    for  i=1,5  do
        print(math.random(1,10))
end

输出结果:

1
3
1
1
4

1.4 math.abs

math.abs(X),返回X的绝对值,通常在时间戳的计算上使用率最高
例:

local   n=os.time()
for  k=1,1000000    do
end
local  new=os.time()
print(math.abs(new-n))

输出结果:

6

1.5 math.floor

math.floor(X),函数返回不大于参数X的最大整数
例:

print(math.floor(10,2))         --输出结果:10
print(math.floor(-10,-2))       --输出结果:-10

1.6 math.ceil

math.ceil(X).函数返回不小于参数X的最大整数

print(math.ceil(5,10))         --输出结果:5
print(math.ceil(-10,-6))       --输出结果:-10

1.7 math.sqrt

math.sqrt(X),函数返回参数X的平方根或1/2方根,如果X为负,产生域错误。

print(math.sqrt(16))       --输出结果:4.0
print(math.sqrt(-16))      --输出结果:-nan

1.8 math.fmod

math.fmod(X,Y),返回参数X/Y的余数

print(math.fmod(45,60))         --输出结果:45

1.9 math.modf

math.modf(X),将参数X分割为整数和小数,两个返回值

print(math.modf(10,6))          --输出结果:10      0.0

1.10 math.max

math.max(X…),函数返回所有参数的最大值

print(math.max(10,5,2,6,62))    --输出结果:62

1.11 math.min

math.min(X…),函数返回所有参数的最小值

print(math.min(10,5,2,6,62))     --输出结果:2

2 Lua标准库之string库

2.1 string库概念

Lua解释器对字符串的支持有限,一个程序可以创建字符并连接字符串,但是不能截取字符,检查字符串的大小、内容等,操作字符串的功能基本来自于string库

2.2 string.len

string.len(s):返回字符串s的长度

local sString="124dhvjghjgnvjgn457"
print("sString字符串的长度为:"..string.len(sString))

输出结果:

sString字符串的长度为:19

2.3 string.format

string.format(s,i):十进制’d’,十六进制’x’,八进制’o’,浮点数’f’

d=5;y=9;g=6
print(string.format("%02d/%02d/%04d",d,y,g))     --05/09/0006

结合模式串使用

local s="hello %d  world  %s"
print(string.format(s,1,"!"))

输出结果:

hello 1  world  !

2.4 string.find

string.find(s,i):查找字符串s和i字符串相对应的位置,返回起始和结束索引值

s="hello world"
i,j=string.find(s,"hello")
print(i,j)
print(string.sub(s,i,j))
print(string.find(s,"world"))
i,j=string.find(s,"1")
print(i,j)
print(string.find(s,"11"))

输出结果:

1	5
hello
7	11
nil	nil
nil

2.5 string.gsub

string.gsub(s,i,j)
s:目标字符(要被操作的字符串)
i:模式串(即将要被替换掉的字符串)
j:替换串(要替换i的字符串)

s=string.gsub("Hello world","world","people")
print(s)          --Hello people

s=string.gsub("hello","l","m")
print(s)          --hemmo

print(string.gsub("hello,people,hello-world","%A","."))        --hello.people.hello.world	3

3 Lua标准库之table库

3.1 table库概念

由一些操作table的辅助函数组成:
作用1:对Lua中的表的大小给出一个合理解释,如:getn(取表长),#table(取表长)
作用2:提供一些插入删除元素以及元素排序的函数,如:insert(向表指定位置插key),remove(删除表的指定位),sort(默认升序排序)

3.2 取表长

1.#:返回表的长度
2.Table.getn(t):返回表中元素个数

local t={1,5,6,8,9,4,5,7}
print(#t)      --8
print(table.getn(t))       --在Lua5.3中不可使用

3.3 table.insert

1.不带参数,默认插入位置为最后一位

local t={1,2,5}
table.insert(t,20)
for i,v in pairs(t) do
print("第"..i.."个元素的值为:"..v..".")
end

输出结果:

第1个元素的值为:1.
第2个元素的值为:2.
第3个元素的值为:5.
第4个元素的值为:20.

2.带参数,插入指定位置

local t={1,2,5}
table.insert(t,2,20)
for i,v in pairs(t) do
print("第"..i.."个元素的值为:"..v..".")
end

输出结果:

第1个元素的值为:1.
第2个元素的值为:20.
第3个元素的值为:2.
第4个元素的值为:5.

3.4 table.remove

1.不带参数,默认删除位置为最后一位

local t={1,2,5}
table.remove(t)
for i,v in pairs(t) do
print("第"..i.."个元素的值为:"..v..".")
end

输出结果:

第1个元素的值为:1.
第2个元素的值为:2.

2.带参数,删除指定位置

 local t={1,2,5}
table.remove(t,2)
for i,v in pairs(t) do
print("第"..i.."个元素的值为:"..v..".")
end  

输出结果:

第1个元素的值为:1.
第2个元素的值为:5.

3.5 table.sort

1.不带排序函数:默认升序

local s=""
local t={1,6,8,9,5,4,7,2}
table.sort(t)
for i=1,#t  do
    s=s..","..t[i]
end
print(s)
s=""
table.sort(t,function(a,b) return a>b end)
for i=1,#t  do
 s=s..","..t[i]
end
print(s)

输出结果:

,1,2,4,5,6,7,8,9
,9,8,7,6,5,4,2,1

2.带排序函数,自定义函数

3.6 table.concat

table,concat(表名,分割符)

local s=""
local t={4,5,2,6,3,1,8}
print(type(table.concat(t,".")))
print(table.concat(t,"."))
print(table.concat(t,"\t"))
print(table.concat(t,"*****"))

输出结果:

string
4.5.2.6.3.1.8
4	5	2	6	3	1	8
4*****5*****2*****6*****3*****1*****8

4 Lua标准库之os库

4.1 os库概念

通常指系统时间的一些取值操作

4.2 时间的3种格式

1.用数值表示时间值
例:

d=12546266

注:这里的12546266是一个以秒为单位的格林威治时间
2.用字符串表示时间
例:

d="2020-11-26 10:25:20"     --2020年11月26日  10点25分20秒
d="11/26/2020  10:25:20"    --2020年11月26日  10点25分20秒

3.时间的列表格式:用table对象来表示时间
例:

d={year=2020,month=11,day=26,hour=10,min=25,sec=20,isdst=false}    --2020年11月26日  10点25分20秒

注:isdst=false表示不使用夏令时,可以通过d.year=2020来访问时间列表

4.3 取得数值(number)格式的时间值

time=os.time()        --返回一个标准的number(数值)格式的时间值

注:os.time()返回的时间是以秒为单位的。
1.获取当前时间数值
例:

time=os.time();
print(time)           --1608022421

2.通过table参数指定时间,获取指定的时间数值
例:

local tab={year=2020,month=11,day=26,hour=10,min=25,sec=20,isdst=false}
print(os.time(tab))

输出结果:

1606386320

4.4 获取列表格式的时间值

tab=os.date("*t",time)

将一个数值格式的时间转换为字符串或者列表(第一个参数指定返回的格式,第二个参数指定一个时间数值)
1.获取当前时间的table格式

tab=os.date("*t")
print(tab)
for k,v in pairs(tab) do
    print(k,v)
end

输出结果:

table: 0x12fe720
min	21
sec	57
month	12
yday	352
wday	5
year	2020
day	17
hour	2
isdst	false

2.通过时间数值,获取指定时间的table格式

 local tab=os.date("*t",7852136945)
 print(os.time(tab))

输出结果:

7852136945

4.5 获取时间的字符串格式

tab=os.date(format,time)

1.获取当前时间的字符串格式
例:

str=os.date("*x")
print(str)                       --*x

2.通过时间数值,获取指定时间的字符串格式
例:

str=os.date("*x",1234589621)
print(str)                     --*x

str=os.date("%x",1234589621)
print(str)                     --02/14/09

3.获取时间的指定部分字符串格式
例:

str=os.date("%x")
print(str)                     --12/17/20

4.6 计算时间值间隔

int =os.difftime(t2,t1)             --t2,t1都是数值格式的时间值

例:

local tab={year=2020,month=11,day=16,hour=10,min=25,sec=20,isdst=false}
t1=os.time(tab)
tab.day=tab.day+1;
t2=os.time(tab)
int=os.difftime(t2,t1);
print(int.."秒时间差")       --86400.0秒时间差

4.7 系统时钟

os.tick():读取系统时钟,以毫秒为单位,表示从系统启动到当前时刻所过去的毫秒数
os.clock():读取系统时钟,以毫秒为单位,表示从系统启动到当前时刻所过去的秒数

print(os.clock())      --0.000966

注:因为内部实现的差异,所以os.tick()与os.clock()返回值并不完全一致

4.8 os.time()

1.不带参数(返回当前计算机时钟至1970年1月1日8时0分0秒的秒数数值)

print(os.time())      --1608173284

2.带参数

print(os.time{year=2020,month=11,day=26,hour=10})    --1606384800
print(os.time())      --1608173436

注:带参数的year,month,day 三个参数必须要写,其他未写则默认为12:00:00

4.9 os.date()

1.不带参数

print(os.date)      --function: 0x422600
print(os.date())    --Thu Dec 17 02:53:33 2020

2.第一个参数
格式化字符串:如*t,%d 等

print(os.date('%X'))        --02:59:00     //返回格式为小时:分钟:秒数
print(os.date('%x'))        --12/17/20    //返回格式为月份/日期/年份

3.第二个参数
时间的数字

print(os.date("%x",9812365421))      --12/10/80
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值