Lua 在同一行打印

发布于 2024-11-30 16:40:21 字数 311 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

删除→记忆 2024-12-07 16:40:21

使用 io.write 而不是 print,这意味着简单的用途,比如调试。

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

懒的傷心 2024-12-07 16:40:21

扩展 lhf 的正确答案io< /code> 库是生产使用的首选。

基础库中的 print 函数是作为原始功能实现的。它允许快速而肮脏的脚本来计算某些内容并打印答案,而对其演示几乎没有控制。它的主要好处是它将所有参数强制为字符串,并使用制表符分隔输出中的每个参数并提供换行符。

当需要对输出进行详细控制时,这些优点很快就会变成缺陷。为此,您确实需要使用 io.write。如果您在同一个程序中混合 printio.write,您可能会遇到另一个缺陷。 print 显式使用 C stdout 文件句柄。这意味着,如果您使用 io.output 更改输出文件句柄,io.write 会执行您期望的操作,但 print 不会。

一个好的折衷方案是用 io.write 实现 print 的替代。它可能看起来像这个未经测试的示例一样简单,其中我尝试清晰地编写而不是最佳地编写,并且仍然“正确”处理 nil 参数:

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

一旦您实现了自己的 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 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.

萤火眠眠 2024-12-07 16:40:21

作为替代方案,只需构建字符串,然后使用单个 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.

橘虞初梦 2024-12-07 16:40:21

您可以使用变量“Hello”和“World”。然后稍后将它们连接起来。像这样:

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

print(h..w)

在本例中,它将显示为“HelloWorld”。但这很容易解决。希望这有帮助!

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!

孤独患者 2024-12-07 16:40:21

添加@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.

窗影残 2024-12-07 16:40:21
local h = "Hello"
local w = "World!"

print(h, w)
local h = "Hello"
local w = "World!"

print(h, w)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文