当前位置: 首页 > 工具软件 > Lua Fun > 使用案例 >

lua 常用内置函数

汤念
2023-12-01

lua 常用内置函数

      

                   

                                 

_VERSION 变量

        

查看当前lua的版本号

 A global variable (not a function) that holds a string 
containing the running Lua version. The current value 
of this variable is "Lua 5.4".
# _VERSION是全局变量,不是函数,保存了当前lua的版本号

         

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 当前lua的版本号为5.4
> _VERSION
Lua 5.4

       

            

                                 

type 函数

        

查看数据类型:type(variable_name)

Returns the type of its only argument, coded as a string. 
# 以字符串的形式返回结果

The possible results of this function are 
"nil" (a string, not the value nil), 
"number", "string", "boolean", "table", 
"function", "thread", and "userdata". 
# 可能的结果为:nil(字符串)、number、string、boolean
#             table、function、thread、userdata

                

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
 # nil类型
> type(nil)
nil
 
# number类型
> type(2)
number
 
# string类型
> type("gtlx")
string
 
> type("2")
string
 
# function类型
> function fun() return 1 end
> type(fun)
function
 
# table类型
> t={}
> type(t)
table

         

                 

                                 

print 函数

        

将信息打印到控制台:print(...)

Receives any number of arguments and prints their values to stdout, 
converting each argument to a string following the same rules of tostring.
# 接收任意数量的参数,将参数转换为字符串打印到控制台

The function print is not intended for formatted output, but 
only as a quick way to show a value, for instance for debugging. 
For complete control over the output, use string.format and io.write. 
# print不用于格式化输出,只用于快速打印结果到控制台
# 格式化输出可用:string.format、io.write

        

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 打印多个参数
> print(1,2,3,4,5,6)
1	2	3	4	5	6

-- 打印table,调用tostring将table转换为string后输出
> t={1,2,3,4,5,6}
> print(t)
table: 0x6000033c8100

         

             

                                 

rawequal 函数

        

比较两个参数是否相等:rawequal(var, var2)

Checks whether v1 is equal to v2, without 
invoking the __eq metamethod. Returns a boolean. 
# 比较v1、v2是否相等,返回boolean值

        

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 比较 1、'1'是否相等:不相等
> a=1 b='1'
> rawequal(a,b)
false

-- 比较1、1是否相等:相等
> c=1 d=1
> rawequal(c,d)
true

        

             

                                 

rawget 函数

        

返回表指定索引的值:rawget(table_name, index)

Gets the real value of table[index], without using the __index 
metavalue. table must be a table; index may be any value. 
# 返回table[index]的值,必须是table类型,index可谓任意值

       

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> t={1,2,3,4,5,6,key='瓜田李下',key2='海贼王'}

-- 返回t[1]
> rawget(t,1)
1

-- key不为数值时,返回nil
> rawget(t,key)
nil

-- 在10处没有值,返回nil
> rawget(t,10)
nil

        

           

                                 

rawlen 函数

        

返回表、字符串的长度:rawlen(var)

Returns the length of the object v, which must be a table or a string, 
without invoking the __len metamethod. Returns an integer. 
# 返回表、字符串的长度,返回值是整数

      

示例:计算表的长度

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> t={1,2,3,4,5,6}

-- 使用'#'返回表的长度
> #t
6

-- 使用rawlen返回表的长度
> rawlen(t)
6

-- 添加表元素
> t[9]=9

-- #、rawlen计算的长度不精确
> #t
6
> rawlen(t)
6

       

示例:计算字符串的长度

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 返回英文字符的长度:一个英文字符占1个字节
> s='gtlx'
> #s
4
> rawlen(s)
4

-- 返回汉字字符的长度:一个汉字字符占3个字节
> s2='瓜田李下'
> #s2
12
> rawlen(s2)
12

       

            

                                 

rawset 函数

        

设置表索引处的值:rawset(table, index, value)

Sets the real value of table[index] to value, without 
using the __newindex metavalue. table must be a table, 
index any value different from nil and NaN, and value any Lua value.
# 设置表索引index处的值为value,必须是table类型,key可为任意类型(除了nil、NaN)

This function returns table
# 函数返回table

         

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> t={1,2,3,4,5,6}
> for key,value in pairs(t) do print(key,value) end
1	1
2	2
3	3
4	4
5	5
6	6

-- 设置索引位8处的值为8
> rawset(t,8,8)
table: 0x600003b98140
> for key,value in pairs(t) do print(key,value) end
1	1
2	2
3	3
4	4
5	5
6	6
8	8

-- key不能为nil,会报错
> rawset(t,key,8)
table index is nil
stack traceback:
	[C]: in function 'rawset'
	stdin:1: in main chunk
	[C]: in ?

> rawset(t,nil,8)
table index is nil
stack traceback:
	[C]: in function 'rawset'
	stdin:1: in main chunk
	[C]: in ?

-- key可为字符串类型
> key='key'
> rawset(t,key,8)
table: 0x600003b98140
> for key,value in pairs(t) do print(key,value) end
1	1
2	2
3	3
4	4
5	5
6	6
8	8
key	8

       

                 

                                 

tostring 函数

        

将参数转换为字符串:tostring(var)

Receives a value of any type and converts it to a string in 
a human-readable format.
# 接收任意类型的参数,将其转换为字符串

If the metatable of v has a __tostring field, then tostring 
calls the corresponding value with v as argument, and uses 
the result of the call as its result. Otherwise, if the metatable 
of v has a __name field with a string value, tostring may use that 
string in its final result.
# 如果元表有__tostring字段,tostring函数调用相关方法返回的结果
# 如果元表有__name字段,tostring函数可能会返回该字段结果

For complete control of how numbers are converted, use string.format. 
# 如果想要格式化输出,可使用string.format

      

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 将table转换为字符串
> t={1,2,3,4,5,6}
> tostring(t)
table: 0x600002c30700

-- 将数字转换为字符串
> a=1
> tostring(a)
1

-- 将函数转换为字符串
> function fun() return 1 end
> tostring(fun)
function: 0x60000393c240

-- 将nil转换为字符串
> tostring(b)
nil

       

            

                                 

tonumber 函数

        

将base进制的参数转换为10进制的数字:tonumber(e, base),base可选,默认10进制

When called with no base, tonumber tries to convert its argument to a number. 
If the argument is already a number or a string convertible to a number, then 
tonumber returns this number; otherwise, it returns fail.
# 没有设置base,转换为10进制(数字的字面量)
# 如果参数是数字、或者数字字符串,转换成功,否则会转换失败(返回nil)

The conversion of strings can result in integers or floats, according to 
the lexical conventions of Lua (see §3.1). The string may have leading and 
trailing spaces and a sign.
# 字符串转换为整型(Integer)、或者实型(Float)

When called with base, then e must be a string to be interpreted as an integer 
numeral in that base. The base may be any integer between 2 and 36, inclusive. 
In bases above 10, the letter 'A' (in either upper or lower case) represents 10,
'B' represents 11, and so forth, with 'Z' representing 35. If the string e is 
not a valid numeral in the given base, the function returns fail. 
# 当带有base时,传入的参数e不需要能被翻译成整数(不能被翻译成整数会报错)
# base可以是2到36之间的任意数字
# base在10进制之上时,A表示10、B表示11、Z可代表35
# 如果根据给定的进制判断e不是有效的字面量,函数转换失败(返回nil)

                  

示例:不带进制转换

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> tonumber('20')
20
> tonumber('220')
220

-- 转换失败返回nil
> tonumber('2q3')
nil

       

示例:带进制转换

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
# 将8进制的字符串转换为10进制输出
> tonumber('20',8)
16

# 将20进制的字符串转换为10进制输出
> tonumber('20',20)
40

# 将21进制的字符串转换为10进制输出
> tonumber('20',21)
42

-- 将35进制的字符串转换为10进制输出
> tonumber('20',35)
70

-- 转换失败返回nil
> tonumber('2a10',6)
nil

-- 35进制时,a被当成合法的数字
> tonumber('2a10',35)
98035

-- 35进制时,e被当成合法的数字
> tonumber('2e10',35)
102935

        

                  

 类似资料: