我正在尝试使用表中每个元素组合执行一个函数。 (在Lua)。表格和元素可以更改,但结构将保持不变。该表是有组织的,因此它的[1]将是函数的第一个参数,依此类推。
如果这是我有的表,
Table = {
[1] = {Player1, Player2}
[2] = {PlayerA, PlayerB, PlayerC}
[3] = {PlayerOne, PlayerTwo}
}
如果我手动编写它,它可能看起来像这样:(假设该函数名为Exe)。
Exe(Player1, PlayerA, PlayerOne)
Exe(Player2, PlayerA, PlayerOne)
Exe(Player3, PlayerA, PlayerOne)
Exe(Player1, PlayerB, PlayerOne)
Exe(Player2, PlayerB, PlayerOne)
Exe(Player3, PlayerB, PlayerOne)
Exe(Player1, PlayerC, PlayerOne)
Exe(Player2, PlayerC, PlayerOne)
Exe(Player3, PlayerC, PlayerOne)
Exe(Player1, PlayerA, PlayerTwo)
Exe(Player2, PlayerA, PlayerTwo)
Exe(Player3, PlayerA, PlayerTwo)
Exe(Player1, PlayerB, PlayerTwo)
Exe(Player2, PlayerB, PlayerTwo)
Exe(Player3, PlayerB, PlayerTwo)
Exe(Player1, PlayerC, PlayerTwo)
Exe(Player2, PlayerC, PlayerTwo)
Exe(Player3, PlayerC, PlayerTwo)
但是,我并不想写出来,这违反了我的一般经验法则,如果你在一个程序中复制和粘贴,那你就错了。
相反,我想通过表格执行每一个可能的组合。这个问题使得表可以(可能)在其中包含任意数量的表,并且表中的表可能具有无限数量的值。
例如,该表可能看起来像这样:
Table = {
[1] = {Player1, Player2}
[2] = {PlayerA}
[3] = {PlayerOne}
}
在哪个执行最终会手动显示:
Exe(Player1, PlayerA, PlayerOne)
Exe(Player2, PlayerA, PlayerOne)
此外,该表最终可能会这样:
Table = {
[1] = {Player1, Player2}
[2] = {PlayerA}
[3] = {PlayerOne}
[4] = {PlayerUno, PlayerDos}
[5] = {PlayerApple, PlayerBoy, PlayerCat, PlayerDog}
}
其中的表演最终会像...
Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerApple)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerApple)
Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerApple)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerApple)
Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerBoy)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerBoy)
Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerBoy)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerBoy)
Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerCat)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerCat)
Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerCat)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerCat)
Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerDog)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerDog)
Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerDog)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerDog)
正如你所看到的,我找到了一个模式......我能够将上面的“执行”事物划分为段/组,例如第1行和第2行有一个更改。然后,它们被复制到第4行和第5行,但下一个变量得到了改变。
正如您所看到的,我无法将该模式放入代码中。我认为将需要一些函数递归,但我不确定如何将其拉出或通过它进行递归。我想我将不得不使用函数...作为参数和解包函数,但我不确定这是如何工作的。
此外,这是必需的原因,而不仅仅是手动复制和粘贴它(实际上会更容易),因为将生成表的内容。
你能帮助我吗?答案 0 :(得分:8)
使用递归。
想象一个函数map_all (fcn, tab, idx, ...)
,它将fcn
映射到tab[1]
前面的所有表格tab[idx]
到...
的元素的乘积
基本情况是idx
小于1时。在这种情况下,只需应用fcn(...)
否则map_all(fcn, tab, idx-1, <el>, ...)
<el>
tab[idx]
function map_all (fcn, tab, idx, ...)
if idx < 1 then
fcn(...)
else
local t = tab[idx]
for i = 1, #t do map_all(fcn, tab, idx-1, t[i], ...) end
end
end
所以,
> Table = {
>> [1] = {'Player1', 'Player2'},
>> [2] = {'PlayerA', 'PlayerB', 'PlayerC'},
>> [3] = {'PlayerOne', 'PlayerTwo'}
>> }
> map_all(print, Table, #Table)
Player1 PlayerA PlayerOne
Player2 PlayerA PlayerOne
Player1 PlayerB PlayerOne
Player2 PlayerB PlayerOne
Player1 PlayerC PlayerOne
Player2 PlayerC PlayerOne
Player1 PlayerA PlayerTwo
Player2 PlayerA PlayerTwo
Player1 PlayerB PlayerTwo
Player2 PlayerB PlayerTwo
Player1 PlayerC PlayerTwo
Player2 PlayerC PlayerTwo
和
> Table = {
>> [1] = {'Player1', 'Player2'},
>> [2] = {'PlayerA'},
>> [3] = {'PlayerOne'}
>> }
> map_all(print, Table, #Table)
Player1 PlayerA PlayerOne
Player2 PlayerA PlayerOne
和
> Table = {
>> [1] = {'Player1', 'Player2'},
>> [2] = {'PlayerA'},
>> [3] = {'PlayerOne'},
>> [4] = {'PlayerUno', 'PlayerDos'},
>> [5] = {'PlayerApple', 'PlayerBoy', 'PlayerCat', 'PlayerDog'},
>> }
> map_all(print, Table, #Table)
Player1 PlayerA PlayerOne PlayerUno PlayerApple
Player2 PlayerA PlayerOne PlayerUno PlayerApple
Player1 PlayerA PlayerOne PlayerDos PlayerApple
Player2 PlayerA PlayerOne PlayerDos PlayerApple
Player1 PlayerA PlayerOne PlayerUno PlayerBoy
Player2 PlayerA PlayerOne PlayerUno PlayerBoy
Player1 PlayerA PlayerOne PlayerDos PlayerBoy
Player2 PlayerA PlayerOne PlayerDos PlayerBoy
Player1 PlayerA PlayerOne PlayerUno PlayerCat
Player2 PlayerA PlayerOne PlayerUno PlayerCat
Player1 PlayerA PlayerOne PlayerDos PlayerCat
Player2 PlayerA PlayerOne PlayerDos PlayerCat
Player1 PlayerA PlayerOne PlayerUno PlayerDog
Player2 PlayerA PlayerOne PlayerUno PlayerDog
Player1 PlayerA PlayerOne PlayerDos PlayerDog
Player2 PlayerA PlayerOne PlayerDos PlayerDog
>