商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

在Lua表中实现迭代values

作者:来自ITPUB论坛  2008-02-15

  【IT168 技术文档】有时你需要写简洁的代码, 当你只需要迭代表的value时ipairs显的有些罗唆, 我们需要更简短的格式. 参看下面2个函数.

  local function valuesIteratorFcn(_s, var_1)   _s.index= _s.index + 1;   return _s.tbl[_s.index];   end   function values(tbl)   local _s = {index=0,tbl=tbl};   return valuesIteratorFcn, _s;   end

  使用上面的代码就可以只迭代表的value了. Discussion of why this works requires careful examination of Lua iterators and how the general for loop works.

  他这样工作:

  for v in values(tbl)   ...   end

  要比这样显得更简洁了:

  for i,v in ipairs(tbl)   ...   end
1
【内容导航】
第1页: 在Lua表中实现迭代values
©版权所有。未经许可,不得转载。
[责任编辑:cynthia]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]