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

loadstring help

Started by Engineer, 09 March 2013 - 07:54 AM
Engineer #1
Posted 09 March 2013 - 08:54 AM
Hello,

I am developing currently a calculator and I am stuck on the output, I cant get it to actually calculate.

It spits this error out: bios:156: bad argument: string expected, got nil

I think (actually im pretty sure) the problem is somewhere between the lines 8 - 43 and 95 - 107 > http://pastebin.com/EN8Y2ChZ

Thanks in advance as always,

Engineer
superaxander #2
Posted 09 March 2013 - 09:00 AM
One thing I know is that loadstring returns a function not a value
Engineer #3
Posted 09 March 2013 - 09:05 AM
One thing I know is that loadstring returns a function not a value

Hoe zou ik dat moeten oplossen?

English: How should I solve that?
superaxander #4
Posted 09 March 2013 - 09:11 AM
Makkelijk vervang het met dit

result = loadstring('return ' .. displayInput)
result = result()

Dan zou het moeten werken.

English:
Easy replace it with this:
<code-snippet>
Then it should work
tesla1889 #5
Posted 09 March 2013 - 09:17 AM
to make it simpler:

result = (loadstring("return " .. displayInput))()
superaxander #6
Posted 09 March 2013 - 09:21 AM
to make it simpler:

result = (loadstring("return " .. displayInput))()
Thought of that but I thought This was easier to understand.
Engineer #7
Posted 09 March 2013 - 09:22 AM
Makkelijk vervang het met dit

result = loadstring('return ' .. displayInput)
result = result()

Dan zou het moeten werken.

English:
Easy replace it with this:
<code-snippet>
Then it should work
to make it simpler:

result = (loadstring("return " .. displayInput))()

Sadly enough, those dont work for me :/
It just attempts to call nil with both
superaxander #8
Posted 09 March 2013 - 09:24 AM
Makkelijk vervang het met dit

result = loadstring('return ' .. displayInput)
result = result()

Dan zou het moeten werken.

English:
Easy replace it with this:
<code-snippet>
Then it should work
to make it simpler:

result = (loadstring("return " .. displayInput))()

Sadly enough, those dont work for me :/
It just attempts to call nil with both
Are you sure displayInput is not nil
Engineer #9
Posted 09 March 2013 - 09:31 AM
I am very sure that displayInput is not nil because I can write(displayInput)
Kingdaro #10
Posted 09 March 2013 - 10:39 AM
I think the problem is that, when you type "=", it makes a string like "return 5 + 5 =", because you're adding the equal sign to the string before doing calculations. Here's a revised snippet of lines 96 to 106:

if tButton[button][5] == "=" then
	local answer = loadstring('return ' .. displayInput)
	if answer then
		result = answer()
	else
		result = 'invalid input'
	end
	displayInput = ""
	resultBool = true
else
	if displayInput == "" then
		displayInput = displayInput .. tButton[button][5]
	else
		displayInput = displayInput .. " " .. tButton[button][5]
	end
end
draw()
It only adds to the displayInput if the sign isn't the equal sign, and otherwise, it does calculations.

The whole 'if answer then' bit up there is to make sure the user doesn't enter in something like "5 + - /", and if he does, the program should tell the user he or she is stupid and should hit him or her self with a brick.
tesla1889 #11
Posted 09 March 2013 - 10:45 AM
–snip–
the program should tell the user he or she is stupid and should hit him or her self with a brick.

please use that direct quote for your error handling :)/>
Engineer #12
Posted 09 March 2013 - 11:12 AM
I think the problem is that, when you type "=", it makes a string like "return 5 + 5 =", because you're adding the equal sign to the string before doing calculations. Here's a revised snippet of lines 96 to 106:

if tButton[button][5] == "=" then
	local answer = loadstring('return ' .. displayInput)
	if answer then
		result = answer()
	else
		result = 'invalid input'
	end
	displayInput = ""
	resultBool = true
else
	if displayInput == "" then
		displayInput = displayInput .. tButton[button][5]
	else
		displayInput = displayInput .. " " .. tButton[button][5]
	end
end
draw()
It only adds to the displayInput if the sign isn't the equal sign, and otherwise, it does calculations.

The whole 'if answer then' bit up there is to make sure the user doesn't enter in something like "5 + - /", and if he does, the program should tell the user he or she is stupid and should hit him or her self with a brick.

Thanks for correcting my code, now it doesnt attempt to call nill, bad argument etc.
But my only problem is now that it just doesnt write anything. I have uploaded the code I have use with your part included on pastebin I am starting to think there is something wrong with something else. Maybe the draw() function is totally wrong.

Hopefully we get there eventually and thanks to everyone who is trying to help me!

Engineer
remiX #13
Posted 09 March 2013 - 01:04 PM
–snip

Fixed version

Okay, so the main reason for it not displaying the result is because when you defined the draw() function, resultBool is false so when ever you call that function, it's ALWAYS detected to be FALSE
Do you understand that? :P/>/>/>

Another problem was that you added a space after every digit/math operation. Now this will cause it to error because when it wants to return a value with a space between the numbers, like this - fixed
local a = '3 + 3 3'
local ans = loadstring('return ' .. a)
print( type( ans ) ) -- output will be nil
-- whereas this will work
local a = '3 + 33'
local ans = loadstring('return ' .. a)
print( ans() ) -- output will be 35

Another problem is that if you press anywhere on the screen that isn't a valid number / operation, it will error. This is because it doesn't verify that the 'button' variable is valid - fixed that for you too :P/>/>/>
Engineer #14
Posted 09 March 2013 - 01:33 PM
Thank you very much! It is finally working :D/>
And to my knowledge 3 + 33 is 36 :P/>

I am just joking about that, thank you ;)/>

Engineer
remiX #15
Posted 09 March 2013 - 10:11 PM
Thank you very much! It is finally working :D/>
And to my knowledge 3 + 33 is 36 :P/>

I am just joking about that, thank you ;)/>

Engineer

Lol derp, thought i had it as 2 + 33 :D/>
Engineer #16
Posted 09 March 2013 - 10:55 PM
This is the proper code: http://pastebin.com/EN8Y2ChZ

Thanks everyone for helping me!:D/>