humter
humter

Reputation: 1

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.

Upvotes: 0

Views: 190

Answers (1)

DarkWiiPlayer
DarkWiiPlayer

Reputation: 7056

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)

Upvotes: 4

Related Questions