Lua 在同一行打印
在 Pascal 中,我有 write 和 writeln 。显然Lua的print
与Pascal的writeln
类似。我们有类似 Pascal 的 write
的东西吗?连续的打印命令如何将其输出发送到同一行?
print("Hello")
print("World")
输出:
Hello
world
我想要这个:
Hello world
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
io.write
而不是print
,这意味着简单的用途,比如调试。Use
io.write
insteadprint
, which is meant for simple uses, like debugging, anyway.扩展 lhf 的正确答案,
io< /code> 库是生产使用的首选。
基础库中的
print
函数是作为原始功能实现的。它允许快速而肮脏的脚本来计算某些内容并打印答案,而对其演示几乎没有控制。它的主要好处是它将所有参数强制为字符串,并使用制表符分隔输出中的每个参数并提供换行符。当需要对输出进行详细控制时,这些优点很快就会变成缺陷。为此,您确实需要使用 io.write。如果您在同一个程序中混合
print
和io.write
,您可能会遇到另一个缺陷。print
显式使用 Cstdout
文件句柄。这意味着,如果您使用 io.output 更改输出文件句柄,io.write 会执行您期望的操作,但 print 不会。一个好的折衷方案是用
io.write
实现print
的替代。它可能看起来像这个未经测试的示例一样简单,其中我尝试清晰地编写而不是最佳地编写,并且仍然“正确”处理nil
参数:一旦您实现了自己的
print
版本, code>,那么您可能会很想以其他方式为您的应用程序改进它。使用比 tostring() 提供的更多格式控制的东西是一个好主意。另一种是考虑使用制表符以外的分隔符。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 tostring
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 mixprint
andio.write
in the same program, you might trip over another defect.print
uses the Cstdout
file handle explicitly. This means that if you useio.output
to change the output file handle,io.write
will do what you expect butprint
won't.A good compromise can be to implement a replacement for
print
in terms ofio.write
. It could look as simple as this untested sample where I've tried to write clearly rather than optimally and still handlenil
arguments "correctly":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 bytostring()
is one good idea. Another is considering a separator other than a tab character.作为替代方案,只需构建字符串,然后使用单个
print
将其写出。您可能并不总是能够访问
io
库。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.您可以使用变量“Hello”和“World”。然后稍后将它们连接起来。像这样:
在本例中,它将显示为“HelloWorld”。但这很容易解决。希望这有帮助!
You could use variables for "Hello" and "World". Then concatenate them later. Like this:
It will be display, in this case, as "HelloWorld". But that's easy to fix. Hope this helped!
添加@Searous 的答案,请尝试以下操作。
本地 h =“你好”
local w = "world"
print(h.." "..w)
您可以将两者连接在一起,只需在两个变量之间连接一个空格即可。
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.