如何在lua中迭代表中的元素对
如何在lua中迭代成对的表元素?我想实现一种无副作用的循环和非循环迭代版本对的方法。
I have table like this:
t = {1,2,3,4}
Desired output of non-circular iteration:
(1,2)
(2,3)
(3,4)
Desired output of circular iteration:
(1,2)
(2,3)
(3,4)
(4,1)
How to iterate over pairs of table elements in lua? I would like to achieve a side-effect free way of circular and non-circular iterating ver pairs.
I have table like this:
t = {1,2,3,4}
Desired output of non-circular iteration:
(1,2)
(2,3)
(3,4)
Desired output of circular iteration:
(1,2)
(2,3)
(3,4)
(4,1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
圆形案例的另一种解决方案
Another solution for the circular case
这是圆形案例
非圆形:
对于更精美的输出,请使用 print(string.format("(%d,%d)",x,y)
Here's the circular case
Non circular:
For fancier output use
print(string.format("(%d,%d)",x,y)
两种情况都没有特殊情况怎么样?
How about having no special case for both cases?