Jump to content




loadstring help


  • You cannot reply to this topic
15 replies to this topic

#1 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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

#2 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 09 March 2013 - 09:00 AM

One thing I know is that loadstring returns a function not a value

#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 09 March 2013 - 09:05 AM

View Postsuperaxander, on 09 March 2013 - 09:00 AM, said:

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?

#4 superaxander

  • Members
  • 609 posts
  • LocationHolland

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

#5 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 09 March 2013 - 09:17 AM

to make it simpler:
result = (loadstring("return " .. displayInput))()


#6 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 09 March 2013 - 09:21 AM

View Posttesla1889, on 09 March 2013 - 09:17 AM, said:

to make it simpler:
result = (loadstring("return " .. displayInput))()
Thought of that but I thought This was easier to understand.

#7 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 09 March 2013 - 09:22 AM

View Postsuperaxander, on 09 March 2013 - 09:11 AM, said:

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

View Posttesla1889, on 09 March 2013 - 09:17 AM, said:

to make it simpler:
result = (loadstring("return " .. displayInput))()

Sadly enough, those dont work for me :/
It just attempts to call nil with both

#8 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 09 March 2013 - 09:24 AM

View PostEngineer, on 09 March 2013 - 09:22 AM, said:

View Postsuperaxander, on 09 March 2013 - 09:11 AM, said:

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

View Posttesla1889, on 09 March 2013 - 09:17 AM, said:

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

#9 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 09 March 2013 - 09:31 AM

I am very sure that displayInput is not nil because I can write(displayInput)

#10 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#11 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 09 March 2013 - 10:45 AM

View PostKingdaro, on 09 March 2013 - 10:39 AM, said:

--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 :)

#12 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 09 March 2013 - 11:12 AM

View PostKingdaro, on 09 March 2013 - 10:39 AM, said:

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

#13 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 09 March 2013 - 01:04 PM

View PostEngineer, on 09 March 2013 - 11:12 AM, said:

--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/>/>

#14 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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

#15 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 09 March 2013 - 10:11 PM

View PostEngineer, on 09 March 2013 - 01:33 PM, said:

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

#16 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 09 March 2013 - 10:55 PM

This is the proper code: http://pastebin.com/EN8Y2ChZ

Thanks everyone for helping me!:D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users