1
votes

I'm trying to setup a loop that prints 1000 lines in a file, but it's printing 50000 lines. It should be adding 5 to i and 2 to c but it's adding 2 to c 100 times, adding 5 to i once then adding 2 to c another 100 times. Here's the code,

local file = io.open("output.txt", "w")
for i=60,5055,5 do
    for c=0,100,2 do
        file:write("  - summon tnt ~" .. c .. " ~" .. c .. " ~" .. c .. " {Fuse:" .. i .. "}\n")
    end 
end
file:close()
3
Yes, that is the purpose of a loop c in a loop i, runs i x c times. ((5055-60)/5) x ((100-0)/2) - PeterMmm

3 Answers

0
votes

Well, your inner loop wil run 50 times for each outer loop, which runs a 1000 times, so 50'000 lines is what you can expect from this code.

0
votes

The math you have for the two loops works out to 50000 file-writes. The first for loop only completes every time the inner loop completes all 50 lines. So if the first loop is running 1000 times, and the inner is going 50 times, 1000 x 50 = 50000.

You could simply remove the inner loop altogether and have code to print 1000 lines to the output file.

0
votes

You need one cycle and inside increase your variables as you want:

--
local c = 0
for i=60,5055,5 do         
     if  c<100 then c = c + 2  else c=0 end
-- write
end

such an idea?