简体   繁体   中英

Why is Lua only printing the first word in a string?

I'm making a simple program in Lua that sends a message, however for some reason it only prints the first word in the string (for example: 'Hello World:' prints 'Hello') This is all the code so far:

local msg = (...)
print('Message:',msg)

I am very new to Lua, I started about a week ago.

When you call a program like

lua some_program.lua foo bar baz

then the program will have three separate string arguments:

local a, b, c = ...
print(a) -- foo
print(b) -- bar
print(c) -- baz

So in your case, the (...) only gives you the first argument.

There's two ways to go about this:

You can make sure the program gets called with only one long string argument by quoting it:

lua some_program.lua "foo bar baz"

or you can take all the arguments you get through ... and concatenate them. This is easily done using table.concat :

local msg = table.concat({...}, " ")
print("Message:", msg)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM