从函数获取多个值而不在 LUA 中创建变量
Getting multiple values from a function without creating a variables in LUA
有没有办法在不为函数创建变量的情况下从函数中获取多个值?
local major, minor, revision, codename = love.getVersion() -- Get current LÖVE version as a string.
因此,我们不会使用四个不同的变量(或数组),而只是 return 您需要的值。
get( love.getVersion(), 0 ) -- Will return the first value (major).
我在某处读到我可以使用方括号并尝试了love.getVersion()[1]
,但它说 "Attempt to index a number value."
这里是函数签名:[https://love2d.org/wiki/love.getVersion] 只是 returns 多个值,如果理解要像你要求的那样实现,你可以对 getVersion 进行包装以具有 lua table 返回如下所示或数组格式
local function getVersion()
local meta_data = {minor_version = "0.1", major_version = "1"}
return meta_data
end
local res = getVersion()
print ("minor_version: ", res['minor_version'])
print ("major_version: ", res['major_version'])
为了示例,我们假设 love.getVersion()
定义如下:
function love.getVersion ()
return 1, 2, 3, "four"
end
使用select(index, ...)
:
如果 index
是数字,则 select
returns index
参数索引之后的所有参数。考虑:
print("A:", select(3, love.getVersion()))
local revision = select(3, love.getVersion())
print("B:", revision)
输出:
A: 3 four
B: 3
如有疑问 - Reference Manual - select
.
使用 table 包装器:
您提到尝试 love.getVersion()[0]
。这就是 几乎,但是您首先需要将返回的值包装成一个实际的 table:
local all_of_them = {love.getVersion()}
print("C:", all_of_them[4])
输出:
C: four
如果您想在一行中完成(本着 "without creating a variables" 的精神),您也需要将 table 括在括号中:
print("D:", ({love.getVersion()})[1])
输出:
D: 1
使用 _
变量:
来自其他语言,您可以使用 _
分配您不感兴趣的值(如果它是一条短平线,没有人会注意到我们创建了一个变量),如:
local _, minor = love.getVersion()
print("E:", minor)
输出:
E: 2
请注意,我在示例中跳过了以下任何 _
(不需要 local _, minor, _, _
)。
有没有办法在不为函数创建变量的情况下从函数中获取多个值?
local major, minor, revision, codename = love.getVersion() -- Get current LÖVE version as a string.
因此,我们不会使用四个不同的变量(或数组),而只是 return 您需要的值。
get( love.getVersion(), 0 ) -- Will return the first value (major).
我在某处读到我可以使用方括号并尝试了love.getVersion()[1]
,但它说 "Attempt to index a number value."
这里是函数签名:[https://love2d.org/wiki/love.getVersion] 只是 returns 多个值,如果理解要像你要求的那样实现,你可以对 getVersion 进行包装以具有 lua table 返回如下所示或数组格式
local function getVersion()
local meta_data = {minor_version = "0.1", major_version = "1"}
return meta_data
end
local res = getVersion()
print ("minor_version: ", res['minor_version'])
print ("major_version: ", res['major_version'])
为了示例,我们假设 love.getVersion()
定义如下:
function love.getVersion ()
return 1, 2, 3, "four"
end
使用select(index, ...)
:
如果 index
是数字,则 select
returns index
参数索引之后的所有参数。考虑:
print("A:", select(3, love.getVersion()))
local revision = select(3, love.getVersion())
print("B:", revision)
输出:
A: 3 four
B: 3
如有疑问 - Reference Manual - select
.
使用 table 包装器:
您提到尝试 love.getVersion()[0]
。这就是 几乎,但是您首先需要将返回的值包装成一个实际的 table:
local all_of_them = {love.getVersion()}
print("C:", all_of_them[4])
输出:
C: four
如果您想在一行中完成(本着 "without creating a variables" 的精神),您也需要将 table 括在括号中:
print("D:", ({love.getVersion()})[1])
输出:
D: 1
使用 _
变量:
来自其他语言,您可以使用 _
分配您不感兴趣的值(如果它是一条短平线,没有人会注意到我们创建了一个变量),如:
local _, minor = love.getVersion()
print("E:", minor)
输出:
E: 2
请注意,我在示例中跳过了以下任何 _
(不需要 local _, minor, _, _
)。