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

lua prompt correction (for results like "sthing, nil, sthing")

Started by aaa, 18 December 2013 - 09:25 PM
aaa #1
Posted 18 December 2013 - 10:25 PM
Using the lua prompt, when we expect a result like 3,nil,a, the prompt only displays 3. This happen for example when receiving an empty message throught rednet, or simply typing '3,nil,"a"' using the prompt. This occurs because once the results of the code are computed, they are printed one after the other until the first nil result, wich is supposed to be the end of the table of results (but not necessarily is) :

Spoiler

if func then
		setfenv( func, tEnv )
		local tResults = { pcall( function() return func() end ) }
		if tResults[1] then
			local n = 1
			while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
				print( tostring( tResults[n + 1] ) )
				n = n + 1
			end
		else
			printError( tResults[2] )
		end
	else
		printError( e )
	end
part of the code found in the rom


This issue can easily be fixed by searching the greatest key of the table of results before printing them :
Spoiler

if func then
		setfenv( func, tEnv )
		local tResults = { pcall( function() return func() end ) }
		if tResults[1] then
			for k,v in pairs(tResults) do
				nForcePrint = math.max(nForcePrint, k - 1)
			end
			for n = 1, nForcePrint do
				 print( tostring( tResults[n + 1] ) )
			end
		else
			printError( tResults[2] )
		end
	else
		printError( e )
	end

I'm not sure I noticed it in the good place, but, IMO, this is a misfunctionnement of the lua prompt and I suggest to fix this default program (as a default program, not by any user rewriting it like I did).
Edited on 18 December 2013 - 09:25 PM
Alice #2
Posted 18 December 2013 - 11:45 PM
Copy the program into a folder, edit it yourself.
If you can do it in Lua, then why do it in Java?
It's not that hard, what, 9/10 lines of code you must edit?
Edited on 18 December 2013 - 10:45 PM
Bomb Bloke #3
Posted 18 December 2013 - 11:56 PM
He's not suggesting ANYTHING be done in Java. He's suggesting that the alteration he's already made be applied to the "official" version of the script.

Anyway, aaa, it may interest you to know that table.maxn(sometable) reliably returns the highest index in a regular numerically indexed table.
Kingdaro #4
Posted 21 December 2013 - 07:21 PM
Copy the program into a folder, edit it yourself.
If you can do it in Lua, then why do it in Java?
It's not that hard, what, 9/10 lines of code you must edit?
I feel like this counts as a bug in a default CC program, and if that's the case, it should be fixed accordingly.
Edited on 21 December 2013 - 06:21 PM
aaa #5
Posted 25 December 2013 - 05:37 PM
I feel like this counts as a bug in a default CC program, and if that's the case, it should be fixed accordingly.

Right, I agree. I did not want to waste time of admins for minor problems (they pay more attention for the bugs' section), but it looks like a bug, though minor. I may try my luck in the proper section.

@Bomb Block. Thx for explaining my thought. As a non native speaker, I may be confusing. thx for table.maxn too, I did not know this function (I only knew #<table> / table.getn).
Edited on 25 December 2013 - 04:38 PM