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

Centered print()

Started by 5tarKaster, 16 October 2012 - 06:40 AM
5tarKaster #1
Posted 16 October 2012 - 08:40 AM
Hello all,

What I want to do here is print out a header to a menu
output should be something like this:
======== Header ========
======== Middle =========

but it throws an exception at the first for loop in the printHead(str) function
! attempt to compare nil with number

any help would be greatly appreciated :D/>/> thanks in advance
its probably a silly mistake too ^^

local w = 20
function menu()
	term.clear()
	print( printHead("Header"))
	print( printHead("Middle"))
end

function printHead(str)
	str2 = ""
	side = w/2 - #str/2

	for i = 1, i > side do
		str2 = str2 + "="
	end

	str2 = str2 + " "..str.." "

	for i = 1, i > side do
		str2 = str2 + "="
	end

	return str2
end

menu()
PonyKuu #2
Posted 16 October 2012 - 08:47 AM
Wrong "for" syntax… It is not C
here is the correct syntax:

for variable = <start>,<stop>,<step> do
	uselessFunction()
	uselessVariable = variable --uselessVariable is useless
end

<step> is optional - if it is not set, it is 1
5tarKaster #3
Posted 16 October 2012 - 08:55 AM
Grr, I knew I made a silly mistake :D/>/> I also saw now that I can't do
 str2 = str2 + "=" 
it sees that as a maths function

I have to concatenate, lol funny word, the string
 str2 = str2.."=" 

thanks for the help :P/>/> I do greatly appreciate it