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

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

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 dolocal v = tostring(select(i,...))
write(v)if i~=n then write'\t'endend
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.

 

Lua print on the same line的更多相关文章

  1. perl unload utf-8 oracle Wide character in print at unload_oracle.pl line 105.

    #!/usr/bin/perl use DBI; use Encode; my $dbName = 'oadb'; my $dbUser = 'vxspace'; my $dbUserPass = ' ...

  2. Wide character in print at a2.pl line 返回json 需要encode_utf8

    centos6.5:/root/test#cat a2.pl use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Heade ...

  3. Wide character in print at a2.pl line 6.

    jrhapt01:/home/tomcat/test> cat a2.pl my $str="$ARGV[0]"; use Encode; use URI::Escape; ...

  4. Wide character in print at hcp.pl line 21.

    jrhmpt01:/root# cat -n hcp.pl 1 use LWP::UserAgent; 2 use Encode; 3 $ua = LWP::UserAgent->new; 4 ...

  5. 为什么出现Wide character in print at a14.pl line 41

    [root@wx03 ~]# cat a14.pl use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; u ...

  6. Lua 基础

    Lua 5.3 的中文手册, http://cloudwu.github.io/lua53doc 在线浏览 --第一部分 -- 两个横线开始单行的注释 --[[ 加上两个[和]表示 多行的注释. -- ...

  7. lua使用io.open跨平台文件夹遍历匹配查找

    -- Desc :实现在LUA_PATH中的lua文件中遍历寻找没用到PNG_PATH路径下的png图片,并将其打印出来. -- Date :12:49:28 2014-09-04 1 print(& ...

  8. lua跨平台文件夹遍历匹配查找

    require"lfs" --[[Desc:在B路径D文件中下 搜寻A路径下的没用到的C类文件: 并且将没用到的B类文件名称打印出来: 设置好路径拖到lua自带编辑器中即可运行之; ...

  9. 理解 Lua 的那些坑爹特性

    按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...

随机推荐

  1. Visual Studio 2012 使用免费的Team Foundation Service

    VS2012提供了在线的TFS服务,免费支持五人小团队,收费情况尚未确定,下面本文演示如何申请和连接在线TFS 服务器. 一.申请TFS服务 首先,打开VS2012,看看是否有团队资源管理器,如果没有 ...

  2. PHP性能分析

    内容来自以下站点整理http://jingyan.baidu.com/article/ff4116257e0d5112e48237a0.html关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言 ...

  3. linux系统版本查看命令

    发布:theboy   来源:net   [大 中 小] 查看linux系统版本的命令 有如下命令可供参考: # lsb_release -a LSB Version:    :core-3.1-ia ...

  4. 有关Mysql连接问题

    问题一——Mysql number Error 2003 MySQL连接错误.错误代码10061.10061一般是Mysql服务没启动.或Mysql服务器无法连接 . 在程序栏找到Mysql\Mysq ...

  5. trade 1.0 开源工具

    dapper.net T4PocoGenerator/ Dapper.ColumnMapper 参考链接: http://blog.csdn.net/ymnets/article/details/85 ...

  6. Lucene 3.0

    http://www.cnblogs.com/forfuture1978/archive/2010/02/22/1671487.html http://www.cnblogs.com/jiekzou/ ...

  7. google查询技巧

    技巧一:使用正确的方法 无论你是使用一个简单或是高级的Google搜索,在此都存在你应该使用的某种可靠的方法.遵循适当的方法你就能获得非常准确的结果:要是忽略这条建议的话,你也许就会看到大量不相关的结 ...

  8. Python 的列表解析和生成表达式的异同

      Python中的列表解析和生成表达式是非常好的特性,他们的形式相似,但是应用场景不太一样. 相似点 列表解析和生成表达式最大的相似点是使用形式: [expr for iter_var in ite ...

  9. 用Tupper自我指涉公式造图

    塔珀自指公式是杰夫·塔珀(Jeff Tupper)发现的自指公式:此公式的二维图像与公式本身外观一样.此公式在众多数学与计算机科学课程里被用作绘制公式图像的练习作业. 公式最初于他2001年SIGGR ...

  10. awk中文手册

    1. 前言 有关本手册 : 这是一本awk学习指引, 其重点着重于 : l        awk 适于解决哪些问题 ? l        awk 常见的解题模式为何 ? 为使读者快速掌握awk解题的模 ...