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. Engine许可初始化 - gis开发初步

    当需要对SDE中的要素类和要素数据集(矢量和栅格)进行编辑时,例如在调用IFeatureDataset的CreateFeatureClass方法时,报错提示: The application is n ...

  2. photoshop如何把阴影分离开(让阴影单独成为一个图层)

    作图的时候经常会用到,给图片加个投影,但有时还满足不了自己的需要,于是可以把投影分离开来单独操作投影. 图层->图层样式->创建图层 有时还需要滤镜->模糊 一下 哈哈,下次忘了来翻 ...

  3. android SDK启动的错误

    1. AVD Manager.exe : failed to execute tools android.bat 请把AVD Manager.exe复制到跟 SDK Manager.exe的相同目录下 ...

  4. eclipse 最全快捷键(网络收集)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...

  5. Linux下的vi编辑器与gcc工具的使用

    最近在网上找了些视频,自学了一点Linux环境下,C编程的方法. 注 vi与vim是有区别的,vim打开的源码文件其中的关键字是有颜色的. vi编辑器有3种模式,命令行模式,插入模式,底行模式. 如果 ...

  6. jQuery Mobile 开发中常见的问题

    目录 页面缩放显示问题 页面跳转后样式丢失js失效 跳转时重复调用pageinit方法的解决办法 如何调用loading效果   1页面缩放显示问题 问题描述: 页面似乎被缩小了,屏幕太宽了. 解决办 ...

  7. 【Qt】Qt Assistant介绍【转】

    简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...

  8. eclipse增加浏览器chrome

    1.安装完了google chrome游览器后,如何让eclipse直接用chrome打开jsp 2.添加到eclipse环境中即可, Window(菜单) -- preferences 增加成功后, ...

  9. 折腾了好久的macos+apache+php+phpmyadmin 终于成功了!

    由于最近需要布置mantis用来进行bug追踪,在此记录其过程. 由于PHP apache环境在Mac OS上是自带的,所以不需要另处下安装包,只需要简单配置一下即可. 首先打开终端输入命令: sud ...

  10. sql2008还原问题

    2003的服务器系统不知是因为对SSD的支持不好还是别的原因,系统使用有很多奇怪的问题,所以就升级一下系统,在备份数据库的时候出现了各种问题 源数据库与现数据库都为sqlserver2008,并都安装 ...