根据赋予函数Lua的变量选择要写入的数组(Select which array to write to based on variables given to a function, Lua)

我正在尝试运行此代码:

function calcs.grps(Number,ion_color) grp .. ion_color .. Y[Number] = (ion_py_mm) grp .. ion_color .. Z[Number] = (ion_pz_mm) end

在一个Lua脚本中,数组已经存在(例如grp2Y,grp5Z等),我想使用这个函数根据输入的两个变量填充它们。我不断得到错误''='预计在'...'附近。 我究竟做错了什么?

为了充实它:

我在模拟中'飞'了120个离子。 这实际上是12组10个离子。 10个单独的组由ion_color区分,ion_color是1到12的整数值。变量'Number'每次循环1到10,然后再转到下一个颜色。 一旦我填充了这些数组,我想获得每个组的标准偏差。

谢谢!

I am trying to run this code:

function calcs.grps(Number,ion_color) grp .. ion_color .. Y[Number] = (ion_py_mm) grp .. ion_color .. Z[Number] = (ion_pz_mm) end

in a Lua script, the arrays already exist (eg grp2Y,grp5Z etc) and I want to use this function to populate them based upon the two variables fed in. I keep getting the error ' '=' expected near '..' '. What am I doing wrong?

To flesh it out a bit:

I am 'flying' 120 ions in my simulation. This is actually 12 groups of 10 ions. The individual groups of 10 are distinguished by ion_color, which is an integer value from 1 to 12. The variable 'Number' just cycles through 1 to 10 each time before moving on to the next color. Once I have populated these arrays I want to get the standard deviation for each group.

Thank you!

最满意答案

您不能“构造”变量名称,但可以构造索引。 使用两级嵌套表。

function calcs.grps(Number,ion_color) ion['grp' .. ion_color .. 'Y'][Number] = (ion_py_mm) ion['grp' .. ion_color .. 'Z'][Number] = (ion_pz_mm) end

嗯,实际上你可以,因为所有的全局变量都只是_G表中的条目,但是不要这样做,因为它很糟糕 - 它是不可读的,使东西泄漏到你不想要的其他函数,等等。

You can't "construct" name of variable, but you can construct an index. Use two levels of nested tables.

function calcs.grps(Number,ion_color) ion['grp' .. ion_color .. 'Y'][Number] = (ion_py_mm) ion['grp' .. ion_color .. 'Z'][Number] = (ion_pz_mm) end

Well, actually you can, since all global variables are just entries in _G table, but don't do that since it is bad - it is unreadable, makes stuff spill to other functions you didn't intend to, etc.

根据赋予函数Lua的变量选择要写入的数组(Select which array to write to based on variables given to a function, Lua)

我正在尝试运行此代码:

function calcs.grps(Number,ion_color) grp .. ion_color .. Y[Number] = (ion_py_mm) grp .. ion_color .. Z[Number] = (ion_pz_mm) end

在一个Lua脚本中,数组已经存在(例如grp2Y,grp5Z等),我想使用这个函数根据输入的两个变量填充它们。我不断得到错误''='预计在'...'附近。 我究竟做错了什么?

为了充实它:

我在模拟中'飞'了120个离子。 这实际上是12组10个离子。 10个单独的组由ion_color区分,ion_color是1到12的整数值。变量'Number'每次循环1到10,然后再转到下一个颜色。 一旦我填充了这些数组,我想获得每个组的标准偏差。

谢谢!

I am trying to run this code:

function calcs.grps(Number,ion_color) grp .. ion_color .. Y[Number] = (ion_py_mm) grp .. ion_color .. Z[Number] = (ion_pz_mm) end

in a Lua script, the arrays already exist (eg grp2Y,grp5Z etc) and I want to use this function to populate them based upon the two variables fed in. I keep getting the error ' '=' expected near '..' '. What am I doing wrong?

To flesh it out a bit:

I am 'flying' 120 ions in my simulation. This is actually 12 groups of 10 ions. The individual groups of 10 are distinguished by ion_color, which is an integer value from 1 to 12. The variable 'Number' just cycles through 1 to 10 each time before moving on to the next color. Once I have populated these arrays I want to get the standard deviation for each group.

Thank you!

最满意答案

您不能“构造”变量名称,但可以构造索引。 使用两级嵌套表。

function calcs.grps(Number,ion_color) ion['grp' .. ion_color .. 'Y'][Number] = (ion_py_mm) ion['grp' .. ion_color .. 'Z'][Number] = (ion_pz_mm) end

嗯,实际上你可以,因为所有的全局变量都只是_G表中的条目,但是不要这样做,因为它很糟糕 - 它是不可读的,使东西泄漏到你不想要的其他函数,等等。

You can't "construct" name of variable, but you can construct an index. Use two levels of nested tables.

function calcs.grps(Number,ion_color) ion['grp' .. ion_color .. 'Y'][Number] = (ion_py_mm) ion['grp' .. ion_color .. 'Z'][Number] = (ion_pz_mm) end

Well, actually you can, since all global variables are just entries in _G table, but don't do that since it is bad - it is unreadable, makes stuff spill to other functions you didn't intend to, etc.