Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 迭代列表的特定条目_List_Lua_Iteration_Lua Table - Fatal编程技术网

List 迭代列表的特定条目

List 迭代列表的特定条目,list,lua,iteration,lua-table,List,Lua,Iteration,Lua Table,给出了一个如下表: t = {mon, tue, wed, thu, fri, sat, sun, spe, btn1, btn2, btn3, btn4, btn5, bar1, bar2, bar3, some_other_stuff, ... } -- assumed that all entries are existing objects 现在我想遍历所有的“day”对象,并给它们一个回调 是否有类似(伪)的内容: 如您所见,我想迭代该表的特定部分。这可能吗? 在我的示例中,这并不

给出了一个如下表:

t = {mon, tue, wed, thu, fri, sat, sun, spe, btn1, btn2, btn3, btn4, btn5, bar1, bar2, bar3, some_other_stuff, ... } 
-- assumed that all entries are existing objects
现在我想遍历所有的“day”对象,并给它们一个回调

是否有类似(伪)的内容:

如您所见,我想迭代该表的特定部分。这可能吗? 在我的示例中,这并不是必需的,因为回调非常简单。
但我想做一个工厂的建设者,这是徒手磨我的齿轮。

呃,没关系,就是这么简单:

for _,v in pairs({t.mon, t.tue, t.wed, ...})do
   -- do your job
end
虽然已经足够了,但更有效的方法是定义一个新表:

do
    local days = { 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun' }
    for _, v in ipairs( dayes ) do
       t[v].callback = function()
          foo(bar)
       end
    end
end
do
    local days = { 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun' }
    for _, v in ipairs( dayes ) do
       t[v].callback = function()
          foo(bar)
       end
    end
end