This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Luapix's profile picture

'<name>' expected in basic for loop

Started by Luapix, 11 July 2014 - 07:05 PM
Luapix #1
Posted 11 July 2014 - 09:05 PM
This is a very simple piece of code, but I can't figure out why it doesn't work.
It doesn't interact with anything else in the code, so I don't think it's necessary to put the rest of the code in this topic.

39	 local srcWidth = term.getSize()
40	 for 1,srcWidth do
41	   io.write(" ")
42	 end

This piece of code is supposed to write a full line of spaces to color the line.
It gives me this error : 40: '<name>' expected

I'm sure the answer is very simple, but I can't figure out what goes wrong.
Also srcWidth is not defined anywhere else in the code.

Thank you for helping.


Edit : Just realized my very stupid mistake. A moderator can delete this topic as it is pretty much useless to anyone that knows Lua.
Edited on 11 July 2014 - 07:10 PM
Lyqyd #2
Posted 11 July 2014 - 10:13 PM
For reference, the mistake here is that the for loop needs a variable to iterate. Changing it to this would fix the code:


for i = 1, srcWidth do
TheOddByte #3
Posted 12 July 2014 - 09:12 PM
Just a tip, You could use string.rep for this instead

local w, h = term.getSize()
local line = string.rep( w, " " )
io.write( line )
this is better because it wouldn't cause flickering to the screen if you would draw alot, so instead of drawing 51 times( that's the normal width of the terminal) it would only draw one time
Luapix #4
Posted 13 July 2014 - 12:40 PM
Just a tip, You could use string.rep for this instead

local w, h = term.getSize()
local line = string.rep( w, " " )
io.write( line )
this is better because it wouldn't cause flickering to the screen if you would draw alot, so instead of drawing 51 times( that's the normal width of the terminal) it would only draw one time

Thank you for the advice !