块列表LUA 5.1.5

发布于 2025-01-17 22:38:20 字数 205 浏览 0 评论 0原文

我正在使用 Lua 5.1.5 并尝试将表切成块(?)。我正在尝试将表格分解成这样的内容:

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

有人知道如何做到这一点吗?

编辑:忘记了 Lua 有表格而不是列表,所以替换了那些

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2025-01-24 22:38:20

改编这段代码:

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

关键点是函数unpack,它是 Lua 5.1 中的全局函数,但在 Lua 5.2+ 中驻留在 table 中。

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+.

时光沙漏 2025-01-24 22:38:20

此代码不尾随 nil,基于 @lhf anser

-- set up unpack to be compatible with old and new versions of lua
local unpack = unpack or table.unpack

-- create a table of {1: 1, 2: 2, ...}
t = {}
n = 34
for i=1,n do t[i]=i end

-- print out chunks
chunk_size = 4
for i=1,#t,chunk_size do
    print(unpack(t, i, math.min(#t, i + chunk_size - 1)))
end

输出

1   2   3   4
5   6   7   8
9   10  11  12
13  14  15  16
17  18  19  20
21  22  23  24
25  26  27  28
29  30  31  32
33  34

This code doesn't trailing nil, based on @lhf anser

-- set up unpack to be compatible with old and new versions of lua
local unpack = unpack or table.unpack

-- create a table of {1: 1, 2: 2, ...}
t = {}
n = 34
for i=1,n do t[i]=i end

-- print out chunks
chunk_size = 4
for i=1,#t,chunk_size do
    print(unpack(t, i, math.min(#t, i + chunk_size - 1)))
end

Output

1   2   3   4
5   6   7   8
9   10  11  12
13  14  15  16
17  18  19  20
21  22  23  24
25  26  27  28
29  30  31  32
33  34
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文