50

In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we have something similar to write of Pascal? How can consecutive print commands send their output to the same line?

print("Hello")
print("World")

Output:

Hello
world

I want to have this:

Hello world
Tadeusz A. Kadłubowski
  • 8,047
  • 1
  • 30
  • 37
AlexStack
  • 16,766
  • 21
  • 72
  • 104

6 Answers6

92

Use io.write instead print, which is meant for simple uses, like debugging, anyway.

lhf
  • 70,581
  • 9
  • 108
  • 149
26

Expanding on lhf's correct answer, the io library is preferred for production use.

The print function in the base library is implemented as a primitive capability. It allows for quick and dirty scripts that compute something and print an answer, with little control over its presentation. Its principle benefits are that it coerces all arguments to string and that it separates each argument in the output with tabs and supplies a newline.

Those advantages quickly become defects when detailed control of the output is required. For that, you really need to use io.write. If you mix print and io.write in the same program, you might trip over another defect. print uses the C stdout file handle explicitly. This means that if you use io.output to change the output file handle, io.write will do what you expect but print won't.

A good compromise can be to implement a replacement for print in terms of io.write. It could look as simple as this untested sample where I've tried to write clearly rather than optimally and still handle nil arguments "correctly":

local write = io.write
function print(...)
    local n = select("#",...)
    for i = 1,n do
        local v = tostring(select(i,...))
        write(v)
        if i~=n then write'\t' end
    end
    write'\n'
end

Once you are implementing your own version of print, then it can be tempting to improve it in other ways for your application. Using something with more formatting control than offered by tostring() is one good idea. Another is considering a separator other than a tab character.

Community
  • 1
  • 1
RBerteig
  • 41,948
  • 7
  • 88
  • 128
  • 3
    This can also be written as `function print(...) io.write(table.concat({...},"\t"),"\n") end` at the cost of creating a table and performing a concatenation. – lhf Aug 22 '11 at 21:26
  • 2
    But only if all arguments are strings or numbers since [`table.concat`](http://www.lua.org/source/5.1/ltablib.c.html#tconcat) calls `lua_isstring()` on each element of the table... compared to the base library `print` which does call `tostring` on each parameter passed. – RBerteig Aug 22 '11 at 21:33
  • @Rberteig: whay do you use select("#",...) instead of just #arg? – AlexStack Aug 23 '11 at 07:41
  • 1
    The `arg` table was a feature of Lua 5.0, and present but deprecated in 5.1, where it only exists if referenced in a function. Referencing it causes a new table to be created since `...` is a very special value that is not a table. Also, `select('#',...)` gets the correct length even if some arguments are `nil` where `#arg` might not. – RBerteig Aug 23 '11 at 18:12
12

As an alternative, just build up your string then write it out with a single print

You may not always have access to the io library.

sylvanaar
  • 8,096
  • 37
  • 59
6

You could use variables for "Hello" and "World". Then concatenate them later. Like this:

local h = "Hello"
local w = "World"

print(h..w)

It will be display, in this case, as "HelloWorld". But that's easy to fix. Hope this helped!

Searous
  • 61
  • 1
  • 1
1

Adding on to @Searous's answer, try the following.

local h = "hello" local w = "world"

print(h.." "..w)

You can concatenate both together, just concatenate a space between both variables.

0
local h = "Hello"
local w = "World!"

print(h, w)
Ray
  • 1