如何在lua中迭代表中的元素对

Mar*_*mro 3 lua loops lua-table

如何迭代lua中的表元素对?我想实现一种无副作用的循环和非循环迭代ver对.

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)
Run Code Online (Sandbox Code Playgroud)

lhf*_*lhf 6

圆形案例的另一种解决方案

   local n=#t
    for i=1,n do 
      print(t[i],t[i%n+1]) 
    end
Run Code Online (Sandbox Code Playgroud)