リストの from 番目の要素から要素を num 個巡廻する。リストは循環しているものとする。
function loop(list, from, num) local dim = #list local num = num or dim local iGlobal = 0 local iLocal = (from or 1) - 1 return function() iGlobal = iGlobal + 1 iLocal = iLocal % dim + 1 if iGlobal > num then return nil else return iGlobal, iLocal, list[iLocal] end end end -- 確かめる。 sute = {10, 20, 30, 40, 50} for iG, iL, val in loop(sute, 3, 10) do print(iG, iL, val) end
-- これと同じことである。 sute = {10, 20, 30, 40, 50} iGlobal = 1 for i = 3, 12 do local iLocal = (i - 1) % #sute + 1 print(iGlobal, iLocal, sute[iLocal]) iGlobal = iGlobal + 1 end