13 Replies - 9852 Views - Last Post: 12 October 2011 - 12:14 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

How can I get the user-input to work in Lua?

Posted 28 September 2011 - 08:10 AM

When I compile this Lua code in SciTE, it compiles successfully but when I run it I receive an error:

Quote

>lua -e "io.stdout:setvbuf 'no'" "Factor finder.lua"
lua: Factor finder.lua:13: attempt to call global 'read' (a nil value)
stack traceback:
Factor finder.lua:13: in main chunk
[C]: ?
>Exit code: 1


Here is my code.
function finder(number)
	if number < 0 then
		return 1
	end
	print("Finding the factors of " .. number)
	for iii = 0, iii > number/2
	do
		if number%iii == 0 then
			print("Found factor: " .. iii)
		end
	end
end
userInput = read()
retVal = finder(userInput)
if retVal == 1 then
	print("Please enter a positive value next time.")
end


Is This A Good Question/Topic? 0
  • +

Replies To: How can I get the user-input to work in Lua?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: How can I get the user-input to work in Lua?

Posted 29 September 2011 - 08:37 AM

read is not a function in Lua, try io.read() instead.

interesting tid-bit of an idiom that is common in Lua is to assign a namespace encased function like io.read to a local variable

local read = io.read


this speeds things up(no namespace and in local variable vs global) and for common functions prevents you from having to type io.

note: you shouldn't do this with globals because it destroys the reason for a namespace in the first place and it doesn't give you the full boost in speed. also, because locals become upvals they can't be functions can't effect the value of it outside their scope(but can inside)

This post has been edited by ishkabible: 29 September 2011 - 08:52 AM

Was This Post Helpful? 2
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 30 September 2011 - 12:40 AM

Oh ok. I kinda get what you mean . . . So when you assign read to the io.read(), read becomes another word for io.read() or does it get the return of io.read() or something else?

Here is my new code. There's a problem because io.read() assumes that the user is inputting (string) "5", not (number) 5. Is there any way to get input for a number?

function finder(number)
	if number < 0 then
		return 1
	end
	print("Finding the factors of " .. number)
	for iii = 0, iii > number/2
	do
		if number%iii == 0 then
			print("Found factor: " .. iii)
		end
	end
end

function main()
	io.write("Enter a positive real number: ")
	userInput = io.read()
	retVal = finder(userInput)
	if retVal == 1 then
		print("Please enter a positive value next time.")
	end
end

main()




Thanks alot for answering, ishkabible! :D
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: How can I get the user-input to work in Lua?

Posted 30 September 2011 - 02:03 PM

actually Lua has automatic string to number conversion so the "5" vs 5 things isn't an issue.(more on this in a bit, yes you are actually right that it is the number string thing)

about assigning io.read to read: io is a "table" in Lua, it associates values with with other values. Lua has a special syntax for string access of a table, the '.' operator. takes 2 arguments, a table(to the left) and an id(to the right). the id is interpreted as a string such that given a table 'x' and an id 'some_id' x.some_id would be the same as x["some_id"]. functions are values on Lua so you can assign a function to an string in a table or access it.

read = io.read


this just simply takes the function at index "read" out of the table 'io' and assigns it to 'read'

i have to go but i will be back to explain the rest in about an hour.(back)

ya, you can't compare numbers in Lua with <, >, <=, or >=. only arithmetic makes that conversion, sorry about that. there are a few solutions here.

1) use tonumber to convert the string
2) use io.read("*n") to tell io.read to accept a number

you are also going to run into some more issues, namely that your for loop isn't going to work like you think it is. Lua for loops take 3 parts

1) a variable with optional initialization value
2) a limit of this variable
3) an increment for this variable

you have a condition for the second part so Lua looks at that like this

for(int i=0;i<true;++i) {

}



also, why are you using 'iii' rather than 'i'?

This post has been edited by ishkabible: 30 September 2011 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 01 October 2011 - 01:46 AM

I use iii rather than i because if i try to find the variable in a massive for loop, I'll get multiple results before the actual for loop. The string iii is much more rare than i. Don't you think so? True that this is a small for loop, but it's good to keep it a habit.

Doesn't Lua auto assume that iii gets incremented by 1, if you do not specify it? Or is it a bad habit to not specify it?

I'm busy at the moment doing revision for a exam so I don't have time to edit my script but I will do so shortly.
Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 02 October 2011 - 08:08 AM

When you said read = io.read, did you mean read = io.read()?

Here is my current code. It gives me an error "attempt to compare nil with number" at line 8 when I enter any number. The tonumber seems to work because if I do not enter a number, it returns 2 in function finder().

BTW, I wrote this code in the same style as I write C++ in, so if you know of any common errors people make when coding in Lua after doing C++, please let me know. ;)

Quote

ya, you can't compare numbers in Lua with <, >, <=, or >=. only arithmetic makes that conversion, sorry about that.

I don't quite get what you mean. :pinch:

Wish me luck for my exams, lol. They start in 8 hours
exactly . . . :(

This post has been edited by hulla: 02 October 2011 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: How can I get the user-input to work in Lua?

Posted 03 October 2011 - 07:55 PM

Quote

I use iii rather than i because if i try to find the variable in a massive for loop, I'll get multiple results before the actual for loop. The string iii is much more rare than i. Don't you think so? True that this is a small for loop, but it's good to keep it a habit.

that makes no sense to me. use i, j, and k. if you need to nest deeper than that then either 1) you need a new algorithm or 2) if you have to use x, y, z and a, b, c. at 9 layers you have been doing something wrong for about 5 layers ;). also use locals to avoid shadowing, that's a common bug in Lua

Quote

Doesn't Lua auto assume that iii gets incremented by 1, if you do not specify it? Or is it a bad habit to not specify it?

ya it dose but i like to read it. it's ok-ish to use, just personal preference.

read = io.read
i meant what i said, im talking about assigning the function to variable basically, like a function pointer :)

Quote

"attempt to compare nil with number"

i would have to see your code, can't tell from just that

Quote

I wrote this code in the same style as I write C++ in

bad in and of itself. you should write in Lua style, you don't need that main function, it's just clutter.

Quote

I don't quite get what you mean. :pinch:

re-reading. it neither do I. try this:
you can't compare strings to numbers in Lua with, <, <=, >, >=. == and ~= simple return false because the types are different so you can but it's not so useful. only arithmetic automatically converts strings to numbers.

try this:
print("6" + 5)


< <= > >= can only compare like types. comparing a boolean to a number is an error.

This post has been edited by ishkabible: 03 October 2011 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 03 October 2011 - 08:39 PM

Oh so you're saying that if I add the string by one and then subtract it by one, it will become a number? I can't actually test it at the moment.
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: How can I get the user-input to work in Lua?

Posted 04 October 2011 - 06:51 AM

yep, that would work but it's better just to use the 'tonumber' function. it's far more readable and probably faster than adding 1 and subtracting 1(although function calls can be slow so im not sure, still it's best to use tonumber). also you can use io.read("*n") to read in a number, it works kinda like scanf in C, you use a format string and it returns all the values. it can even return more than 1 value!

x, y, z = io.read("*n*n*n")


cool right!

also there is tostring which converts other values to strings! also if the meta method "__tostring" exists for a value then tostring calls and returns that.

This post has been edited by ishkabible: 04 October 2011 - 06:54 AM

Was This Post Helpful? 1
  • +
  • -

#10 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 05 October 2011 - 01:15 AM

View Postishkabible, on 04 October 2011 - 09:51 PM, said:

x, y, z = io.read("*n*n*n")


cool right!

Something you don't see in C or C++ :)

Here is my new version of the program but I still get the same error as previously. :(
function finder(number)
	if number == nil then return 2 end
	if number < 0 then return 1 end
	if number % 1 ~= 0 then return 3 end
	print("Finding the factors of " .. number)
	for iii = 0, iii < number/2 do
		if number%iii == 0 then
			print("Found factor: " .. iii)
		end
	end
end


io.write("Enter a positive real number: ")
userInput = io.read("*n")
retVal = finder(userInput)
if retVal == 1 then
	print("Please enter a positive value next time.")
elseif retVal == 2 then
	print("Your entry could not be read as a real number.")
elseif retVal == 3 then
	print("Your entry is not an integer")
end


I have a feeling I'm doing some really obvious mistake but I just can't see what I'm doing wrong.
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: How can I get the user-input to work in Lua?

Posted 05 October 2011 - 06:41 AM

for iii = 0, iii < number/2 do

this is your issue, for loops don't work like that in Lua as i explained before. try this instead.

for i = 1, number/2, 1 do


notice i start at 1, that's because the middle parameter is compared to 'i' with <= so to get that many loops you need to start at 1. arrays also start at 1 rather than at 0 in Lua.
Was This Post Helpful? 1
  • +
  • -

#12 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 11 October 2011 - 01:58 AM

Wow for-loops are confusing in Lua lol.
Is it possible to check for anything other than the condition being less-than-or-equal-to in the condition part of the for-loop?
Was This Post Helpful? 0
  • +
  • -

#13 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: How can I get the user-input to work in Lua?

Posted 11 October 2011 - 08:26 AM

no, you would have to use a while loop for that. you can also use a for-in loop which accepts a function that returns another function that returns some value on each loop. the 'pairs' function in Lua is an example of this.

for k, v in pairs(t) do --"t" is a table
    --k is "key"
    --v is "value"
end



that iterates over a table, it's kinda like a for-each loop
Was This Post Helpful? 1
  • +
  • -

#14 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: How can I get the user-input to work in Lua?

Posted 12 October 2011 - 12:14 AM

Cool, I'll learn about while-loops. Thanks again. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1