如何在Lua中进行程序化迭代相似的变量?

我有很多变量似乎更容易引用,但我不知道该怎么做;变量名与坐标系条目相关,它们写入的位置也是基于坐标的。我尝试搜索谷歌,阅读了Lua文档,但由于我不确定在寻找什么,所以我认为这妨碍了我的搜索。这是代码节选中变量的外观:

-- 银行1

local Object111 =  sItem.getTargetDetails("-4,3,-4")
local Object112 =  sItem.getTargetDetails("-5,3,-4")
local Object113 =  sItem.getTargetDetails("-6,3,-4")
local Object114 =  sItem.getTargetDetails("-7,3,-4")
local Object115 =  sItem.getTargetDetails("-8,3,-4")

local Object121 =  sItem.getTargetDetails("-4,4,-4")
local Object122 =  sItem.getTargetDetails("-5,4,-4")
local Object123 =  sItem.getTargetDetails("-6,4,-4")
local Object124 =  sItem.getTargetDetails("-7,4,-4")
local Object125 =  sItem.getTargetDetails("-8,4,-4")

local Object131 =  sItem.getTargetDetails("-4,5,-4")
local Object132 =  sItem.getTargetDetails("-5,5,-4")
local Object133 =  sItem.getTargetDetails("-6,5,-4")
local Object134 =  sItem.getTargetDetails("-7,5,-4")
local Object135 =  sItem.getTargetDetails("-8,5,-4")

-- 银行2

local Object211 =  sItem.getTargetDetails("-4,3,1")
local Object212 =  sItem.getTargetDetails("-5,3,1")
local Object213 =  sItem.getTargetDetails("-6,3,1")
local Object214 =  sItem.getTargetDetails("-7,3,1")
local Object215 =  sItem.getTargetDetails("-8,3,1")

local Object221 =  sItem.getTargetDetails("-4,4,1")
local Object222 =  sItem.getTargetDetails("-5,4,1")
local Object223 =  sItem.getTargetDetails("-6,4,1")
local Object224 =  sItem.getTargetDetails("-7,4,1")
local Object225 =  sItem.getTargetDetails("-8,4,1")

local Object231 =  sItem.getTargetDetails("-4,5,1")
local Object232 =  sItem.getTargetDetails("-5,5,1")
local Object233 =  sItem.getTargetDetails("-6,5,1")
local Object234 =  sItem.getTargetDetails("-7,5,1")
local Object235 =  sItem.getTargetDetails("-8,5,1")

然后,我会调用这些变量中的每一个在一个函数中,其中屏幕上的位置是名称中的数字的一个函数,前两个数字与x坐标相关,最后一个数字与y坐标相关:

mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
等等....

我可以看到这应该能够动态生成,但我不知道从哪里开始而不手动调用;我希望我的代码更干净。在这个阶段,我真的只需要学会钓鱼;如果有人能把我指向解释我正在尝试的东西的文档,我会非常感激。

点赞
用户2135406
用户2135406

以下是我为那些努力实现相同目标的人提供的最终解决方案:

local Banks = 2
local Rows = 3
local Columns = 5

function Objectstatus(bank,row,column)

    Id = bank .. row .. column
    worldx = "-" .. column+3
    worldy = row+2
    if bank == 1 then worldz = -4; end
    if bank == 2 then worldz = 1; end
    Object = {}
    Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz)
    xcursor = (column*7)+3
    if bank == 1 then
        ycursor = (row*4)+8
    end
    if bank == 2 then
        ycursor = (row*4)+24
    end

    mon.setCursorPos(xcursor,ycursor)
    powertext(Object[Id].StoredPercentage) -- 基于结果设置文本设置的函数
    mon.write(Object[Id].StoredPercentage) -- 实际写入结果
end

    for BankTemp = 1, Banks do
        for ColumnTemp = 1, Columns do
            for RowTemp = 1, Rows do
            Objectstatus(BankTemp,RowTemp,ColumnTemp)
            end
        end
    end
2014-09-01 11:41:59