假如,想写一个循环,依次处理2,3,5,7,11....质数位置的table元素。处理函数是:function kaka(index,ele) print(index,ele)--当然,这里什么也没干end按照文字顺序,我们先写一个循环:for index,ele in generate(table_x) do kaka(table_x[index])end那个generate()函数的任务就是生产一个元组序列:(2,ele2),(3,ele3),(5,ele5).......这件事可以分两步来做,先写一个函数,yield这个序列,在写一个函数驱动这个协程。function cofunc(table_x) coroutine.yield(2,table_x[2]) coroutine.yield(3,table_x[3]) coroutine.yield(5,table_x[5])...end驱动函数:function generate(table_x) 阅读全文>
发表于 @ 2008年12月25日 23:03:00|评论(loading...)|举报|收藏
g='hi~'local g='hello!'for i=1,2 dolocal g=g..'1'print(g)end输出:hello!1hello!1
阅读全文>
发表于 @ 2008年12月25日 14:32:00|评论(loading...)|举报|收藏