Search code examples
lualua-table

Chunking lists lua 5.1.5


I am using Lua 5.1.5 and trying to cut tables into chunks(?). I am trying to break apart a table into something like this:

{{a, b, c ,d}, {e, f, g, h}, ...}

Anyone got an idea on how to do that?

Edit: Forgot Lua had tables instead of lists so replaced those


Solution

  • Adapt this code:

    t={}
    n=34
    local unpack = unpack or table.unpack
    for i=1,n do t[i]=i end
    for i=1,#t,4 do
        print(i,unpack(t,i,i+3))
    end
    

    The key point is the function unpack, which is a global function in Lua 5.1 but resides in table in Lua 5.2+.