Reputation: 896
Given is a table like:
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
Now I want to iterate through all the "day"-objects and give them a callback.
Is there something like (pseudo):
for _,v in pairs(t.{mon,tue,wed,thu,fri,sat,sun,spe})do
v.callback = function()
foo(bar)
end
end
So as you see I want to iterate through a specific part of that table. Is this possible? In my example it's not really neccesary due the callback is very simple. But I'm trying to make a factory constructor and with bare hands this is grinding my gears.
Upvotes: 2
Views: 36
Reputation: 80639
While your solution is sufficient, a more efficient way would be to define a new table:
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
Upvotes: 2
Reputation: 896
Uh nevermind, it's that simple:
for _,v in pairs({t.mon, t.tue, t.wed, ...})do
-- do your job
end
Upvotes: 2