逗号分隔的打印()
2013-12-18 10:16:25
收藏:0
阅读:151
评论:2
希望这不是一个愚蠢的问题,但是当我偶然发现这个问题后我搜索了一下,但是我找不到任何文档来解释这个问题。在print()语句中,逗号(,)的用途是什么?它似乎会在输入之间连接一个制表符。
例如:
print("this" .. "is" .. "string" .. "concatenation");
print("how", "is", "this", "also", "working?");
输出:
this is string concatenation
how is this also working?
我之所以要研究这个问题,是因为它似乎可以允许nil值的连接。
例如2:
local nilValues = nil;
print("This", "somehow", "seems", "to", "concatenate", nilValues);
print("This" .. "will" .. "crash" .. "on" .. nilValues); -- ERROR -> attempt to concatenate local 'nilValues' (a nil value)
输出2:
This somehow seems to concatenate nil
Error: lua: test.lua:7: attempt to concatenate local 'nilValues' (a nil value)
我尝试搜索逗号在字符串连接中的使用,还检查了Lua指南上的print()文档,但我找不到任何解释这个问题的东西。
点赞
用户1290114
print("this" .. "is" .. "string" .. "concatenation");
print("how", "is", "this", "also", "working?");
在第一个 print 语句中,只有一个参数。它是一个字符串 "thisisstringconcatenation"。因为它先进行字符串拼接,然后再传递给 print 函数。
在第二个 print 语句中,有 5 个参数传递给 print。
local nilValues = nil;
print("This", "somehow", "seems", "to", "concatenate", nilValues);
print("This" .. "will" .. "crash" .. "on" .. nilValues);
在第二个例子中,您将一个字符串与一个 nil 值拼接起来,从而导致错误。
2013-06-04 11:20:24
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
print可以接受变量数量的参数,并在打印的项之间插入\t。您可以把它看作是像这样定义的:(虽然实际上并不是这样,但是这个示例代码来源于 Programming in Lua http://www.lua.org/pil/5.2.html)printResult = "" function print (...) for i,v in ipairs(arg) do printResult = printResult .. tostring(v) .. "\t" end printResult = printResult .. "\n" end在例子 2 中:
local nilValues = nil; print("This", "somehow", "seems", "to", "concatenate", nilValues); print("This" .. "will" .. "crash" .. "on" .. nilValues);第一个
print接受多个参数,并将它们逐个打印出来,每个之间都用\t分隔。请注意,print(nil)是有效的,并且将打印nil。第二个
print接受一个参数,即一个字符串。但是字符串参数"This" .. "will" .. "crash" .. "on" .. nilValues是无效的,因为nil不能与字符串连接。