用表进行Lua多重分配 [英] Lua multiple assignment with tables

查看:75
本文介绍了用表进行Lua多重分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

function foo()
    return 1, 2, 3
end

bar = {}

bar = {a, b, c = foo()}

产生:

bar.a = nil
bar.b = nil
bar.c = 1

如何写成这样,这样您就可以得到:

How can this be written so that you get:

bar.a = 1
bar.b = 2
bar.c = 3

无需编写如下内容:

function foo()
    return 1, 2, 3
end

bar = {}
a, b, c = foo()

bar = {a = a, b = b, c = c}

推荐答案

BLUF

没有直接或优雅的方法可以做到这一点.您必须像这样手动进行

There's no straight forward or elegant way to do this. You'll have to do it manually like this

local r = { f() }           --> store all returned values in r
local bar = { }
local c = string.byte 'a'   --> start with 'a'
for _, v in ipairs(r) do
   local t = string.char(c)
   bar[t] = v               --> assign each value to respective letter
   c = c + 1
end

如果有a, b, c = foo(),则将获得分配给三个变量的所有三个值.但是,您已经

If you'd had a, b, c = foo() you'd get all the three values assigned to the three variables. However, you've

bar = { a, b, c = foo() }

表构造函数表达式将被解释为键abc插入到表中,只有最后一个具有关联值的键(并且:没有关联值的键被视为nil;因此,ab决不会插入).由于只有一个变量采用foo返回的值,因此除第一个返回的其他所有内容外,其他所有内容都将被丢弃.

This table constructor expression will get interpreted as the keys a, b, c getting inserted into the table, with only the last key having an associated value (aside: keys with no associated value are taken as nil; hence a and b never get inserted). Since there's only one variable to take the values returned by foo, except the first everything else it returns are discarded.

或者,bar = { foo() }会将foo返回的所有值分配为bar的数组值.但是,访问这些键的键将是[1][2]等,而不是'a''b'等.

Alternatively bar = { foo() } will assign all values returned by foo as array values of bar. However, the key to access these would [1], [2], etc. and not 'a', 'b', etc.

请阅读以下内容,以了解何时返回的值将被丢弃,何时不将其丢弃.

Read below to know when the returned values get discarded and when they don't.

TL; DR 仅当函数调用是表达式列表中的最后一个/唯一表达式时,才保留所有返回的值.除了第一个以外,所有其他地方都将被丢弃.

TL;DR All returned values are retained only when the function call is the last/only expression in a list of expressions; elsewhere all except the first are discarded.

在Lua中,当我们从一个函数返回多个结果时,如果该函数调用本身是一条语句,则所有这些结果都将被丢弃.

In Lua, when we return multiple results from a function, all of them get discarded if the function call is a statement by itself.

foo()

将丢弃所有三个返回值.

will discard all three return values.

如果在表达式中使用它,则只会保留第一个,而所有其他内容将被丢弃.

If it's used in an expression, only the first will be retained and everything else will be discarded.

x = foo() - 1
print(x)        -- prints 0; the values 2, 3 are discarded

表达式列表中的函数调用

仅当调用在表达式列表中显示为最后/唯一项时,才保留返回的整个值列表.这样的表达式列表出现在Lua的四个地方:

Function call in an expression list

The entire list of values returned is retained only when the call appears as the last/only item in a list of expressions. Such list of expressions occur at four places in Lua:

  1. 多次分配

  1. Multiple assignment

例如local a, b, c, d = 0, f().在这里bcd分别获得值123.

E.g. local a, b, c, d = 0, f(). Here b, c, d get the values 1, 2, 3 respectively.

表构造器

例如local t = { 0, f() }. f返回的所有值都放在第一个0之后的t中.

E.g. local t = { 0, f() }. All values returned by f are put into t following the first 0.

函数调用参数

例如g(a, f()). g将收到4个参数,而不是2个参数. af中的三个值.

E.g. g(a, f()). g would receive 4, not 2, arguments. a and the three values from f.

return语句

例如return 'a', f().除了字符串'a'外,f返回的所有值都将在调用端被接收.

E.g. return 'a', f(). Additional to the string 'a', all values returned by f will be received at the calling end.

在所有这些情况下,如果f不是出现在列表中的最后一个表达式或不是唯一的表达式,那么它返回的所有值(第一个表达式除外)都将被丢弃.

In all these situations, had f appeared not as the last expression in the list or wasn't the only expression, then all values it returned except the first would've been discarded.

在多重赋值语句中,当赋值的数量小于变量数量时,多余的变量将赋给nil.相反,如果变量数量较少,则多余的值将被丢弃.

In the multiple assignment statement, when the number of values assigned is lesser than number of variables, the extra variables be assigned to nil. When it's the other way around i.e if the number of variables are lesser, the extra values are discarded.

a, b, c = 1, 2         -- a = 1, b = 2, c = nil
a, b, c = 1, 2, 3, 4   -- 4 gets discarded

这篇关于用表进行Lua多重分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆