Lua print on the same line
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
Useio.write
instead
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的更多相关文章
- 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 = ' ...
- 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 ...
- 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; ...
- 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 ...
- 为什么出现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 ...
- Lua 基础
Lua 5.3 的中文手册, http://cloudwu.github.io/lua53doc 在线浏览 --第一部分 -- 两个横线开始单行的注释 --[[ 加上两个[和]表示 多行的注释. -- ...
- lua使用io.open跨平台文件夹遍历匹配查找
-- Desc :实现在LUA_PATH中的lua文件中遍历寻找没用到PNG_PATH路径下的png图片,并将其打印出来. -- Date :12:49:28 2014-09-04 1 print(& ...
- lua跨平台文件夹遍历匹配查找
require"lfs" --[[Desc:在B路径D文件中下 搜寻A路径下的没用到的C类文件: 并且将没用到的B类文件名称打印出来: 设置好路径拖到lua自带编辑器中即可运行之; ...
- 理解 Lua 的那些坑爹特性
按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...
随机推荐
- java大数--总结
BigInteger(高精度整数) 1.所在包: java.math.BigInteger 2.大数运算,以下返回类型均为BigInteger BigInteger a; BigInteger b; ...
- 网易面试题:和为n连续正数序列
题目: 输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. 继续做些题目,看到这是网易面试题,于是 ...
- 惠普M1005打印机无法自动进纸的问题
惠普M1005打印机无法自动进纸的问题 问题起因 其实我也不太清楚是什么起因,前一天用的好好的惠普M1005打印机,在打印时没有直接打印,会弹出一个提示对话框,同时打印机显示屏上显示“load tra ...
- [精校版]The Swift Programming Language
通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”.在 Swift 中,可以用一行代码实现: println("hello, world") 如 ...
- ASP.NET MVC 2 验证
来源:http://www.cnblogs.com/jhxk/articles/2612885.html 只为把自己觉的好的存起来 对用户输入的验证以及强制业务规则/逻辑是大多数web应用的核心需求 ...
- MYSQL-给带特殊符号的数据库创建用户名
MYSQL-创建数据库及用户名: mysql> create database yoon;Query OK, 1 row affected (0.00 sec) mysql> grant ...
- ios按钮点击后翻转效果
代码是网上找到的,不过找到的时候直接复制下来不能用,稍微整理下,为和我一样水平的菜鸟观摩一下下. (1)引入“QuartzCore.framework”库,头部引用. #include<Quar ...
- mysql5.5 Replication 主从同步
mysql5.5 Replication 主从同步 ------------------[主]------------------[mysqld]server-id=1 log-bin=mysql-b ...
- sky A800s手机恢复出厂设置操作
关机的情况下,手指一起按这3个按键(音量下键+搜索键+开机键)看到SKY标志后即可松手,就可以进入恢复界面恢复界面操作方法:音量上下键为上下移动,关机键为选择.选择 wipe data/factory ...
- VBS基础篇 - class
Class 语句:声明一个类的名称,以及组成该类的变量.属性和方法的定义. Class name '参数name必选项,Class 的名称 statements '一个或多个语句,定义了 Class ...