Getting information from a user in Lua

How can I get user input in Lua (e.g. scanfin C)?
For example, a program asks the user for its name, then it writes down its name, then the program displays its name.

+5
source share
2 answers

Use io.read () Beware that the function can be configured with various parameters. Here are some examples.

 s = io.read("*n") -- read a number
 s = io.read("*l") -- read a line (default when no parameter is given)
 s = io.read("*a") -- read the complete stdin
 s = io.read(7) -- read 7 characters from stdin
 x,y = io.read(7,12) -- read 7 and 12 characters from stdin and assign them to x and y
 a,b = io.read("*n","*n") -- read two numbers and assign them to a and b
+15
source

The simplest input can be obtained with io.read(). This will read one line from standard input (usually a keyboard, but can be redirected, for example, from a file).

You can use it as follows:

io.write('Hello, what is your name? ')
local name = io.read()
io.write('Nice to meet you, ', name, '!\n')

io.read() io.input():read(), io.write() io.output():write(). API read() .

, io.write() , , print().

+4

All Articles