Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在模式中格式化lua中的字符串_Lua_Formatting - Fatal编程技术网

在模式中格式化lua中的字符串

在模式中格式化lua中的字符串,lua,formatting,Lua,Formatting,我想制作一个脚本,它可以接受任何数字,对它们进行计数,并以一种格式返回它们。 就这样 for i = 1,9 do print(i) end 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 会回来的 1 2 3 4 5 6 7 8 9 但是我希望它像这样打印 for i = 1,9 do print(i) end 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 11

我想制作一个脚本,它可以接受任何数字,对它们进行计数,并以一种格式返回它们。 就这样

for i = 1,9 do
print(i)
end
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20
会回来的

1
2
3
4
5
6
7
8
9
但是我希望它像这样打印

for i = 1,9 do
print(i)
end
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20
我希望它能在9个以上的情况下工作,所以像20个这样的情况就是这样

for i = 1,9 do
print(i)
end
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20
我确信可以使用lua中的字符串库来完成,但我不确定如何使用该库


有什么帮助吗?

for循环执行可选的第三步:


for循环执行可选的第三步:


我可以想出两种方法:

local NUMBER = 20
local str = {}
for i=1,NUMBER-3,3 do
    table.insert(str,i.." "..i+1 .." "..i+2)
end
local left = {}
for i=NUMBER-NUMBER%3+1,NUMBER do
    table.insert(left,i)
end
str = table.concat(str,"\n").."\n"..table.concat(left," ")
还有一个使用gsub的:

local NUMBER = 20
local str = {}
for i=1,NUMBER do
    str[i] = i
end
-- Makes "1 2 3 4 ..."
str = table.concat(str," ")
-- Divides it per 3 numbers
-- "%d+ %d+ %d+" matches 3 numbers divided by spaces
-- (You can replace the spaces (including in concat) with "\t")
-- The (...) capture allows us to get those numbers as %1
-- The "%s?" at the end is to remove any trailing whitespace
-- (Else each line would be "N N N " instead of "N N N")
-- (Using the '?' as the last triplet might not have a space)
--    ^ e.g. NUMBER = 6 would make it end with "4 5 6"
-- The "%1\n" just gets us our numbers back and adds a newline
str = str:gsub("(%d+ %d+ %d+)%s?","%1\n")
print(str)
我已经对这两个代码段进行了基准测试。上一个稍微快一点,尽管差别几乎为零:

Benchmarked using 10000 interations
NUMBER   20        20        20        100       100
Upper    256 ms    276 ms    260 ms    1129 ms   1114 ms
Lower    284 ms    280 ms    282 ms    1266 ms   1228 ms

我可以想出两种方法:

local NUMBER = 20
local str = {}
for i=1,NUMBER-3,3 do
    table.insert(str,i.." "..i+1 .." "..i+2)
end
local left = {}
for i=NUMBER-NUMBER%3+1,NUMBER do
    table.insert(left,i)
end
str = table.concat(str,"\n").."\n"..table.concat(left," ")
还有一个使用gsub的:

local NUMBER = 20
local str = {}
for i=1,NUMBER do
    str[i] = i
end
-- Makes "1 2 3 4 ..."
str = table.concat(str," ")
-- Divides it per 3 numbers
-- "%d+ %d+ %d+" matches 3 numbers divided by spaces
-- (You can replace the spaces (including in concat) with "\t")
-- The (...) capture allows us to get those numbers as %1
-- The "%s?" at the end is to remove any trailing whitespace
-- (Else each line would be "N N N " instead of "N N N")
-- (Using the '?' as the last triplet might not have a space)
--    ^ e.g. NUMBER = 6 would make it end with "4 5 6"
-- The "%1\n" just gets us our numbers back and adds a newline
str = str:gsub("(%d+ %d+ %d+)%s?","%1\n")
print(str)
我已经对这两个代码段进行了基准测试。上一个稍微快一点,尽管差别几乎为零:

Benchmarked using 10000 interations
NUMBER   20        20        20        100       100
Upper    256 ms    276 ms    260 ms    1129 ms   1114 ms
Lower    284 ms    280 ms    282 ms    1266 ms   1228 ms

在打印值之前,请使用临时表包含这些值:

local temp = {}
local cols = 3

for i = 1,9 do
    if #temp == cols then
       print(table.unpack(temp))
        temp = {}
    end
    temp[#temp + 1] = i
end

--Last minute check for leftovers
if #temp > 0 then
    print(table.unpack(temp))
end
temp = nil

在打印值之前,请使用临时表包含这些值:

local temp = {}
local cols = 3

for i = 1,9 do
    if #temp == cols then
       print(table.unpack(temp))
        temp = {}
    end
    temp[#temp + 1] = i
end

--Last minute check for leftovers
if #temp > 0 then
    print(table.unpack(temp))
end
temp = nil

谢谢这正是我需要的。谢谢!这正是我所需要的。或者将io.write()组合起来,如果。。。to:io.write(i,i%每行==0和'\n'或'\t')或组合io.write(),如果。。。to:io.write(i,i%每行==0和'\n'或'\t')