Lua io.write() 将不需要的 material 添加到输出字符串
Lua io.write() adds unwanted material to output string
当我启动交互式 Lua shell 时,io.write()
在我希望它打印的字符串后添加不需要的 material。 print()
,但是没有:
[user@manjaro lua]$ lua
Lua 5.4.2 Copyright (C) 1994-2020 Lua.org, PUC-Rio
> io.write('hello world')
hello worldfile (0x7fcc979d4520)
> print('hello world')
hello world
当我在程序中使用 io.write()
时它也能正常工作:
--hello.lua
io.write('hello world\n')
print ('hello world')
输出:
[user@manjaro lua]$ lua hello.lua
hello world
hello world
我在戴尔台式机上使用 Manjaro Linux。谁能告诉我这是怎么回事?提前致谢。
编辑:也许我应该补充一点,不需要的 material 总是这样的:
file (0x7f346234d520)
它总是 'file' 后跟一个看起来像括号中的大十六进制数。确切的数字在一个 shell 会话中保持不变,但在不同的 shell 会话之间变化。
"file (0x7fcc979d4520)
"(或任何地址)是 io.write
调用的 return 值,带有隐式的 tostring
.
lua(1) 手册页说
In interactive mode, lua
prompts the user, reads lines from the standard input, and executes them as they are read. If the line contains an expression or list of expressions, then the line is evaluated and the results are printed.
这里的问题是 io.write('hello world')
既可以是表达式也可以是语句。由于它是一个有效的表达式,解释器输出不需要的 return 值。
作为解决方法,请尝试添加分号:
> io.write('hello world\n');
hello world
虽然 Lua 通常不需要在每行末尾的每个语句都使用分号,但它确实允许使用分号。这里很重要,这意味着语法不能是表达式,只能是调用函数的语句。所以解释器不会输出 returned 值。
当我启动交互式 Lua shell 时,io.write()
在我希望它打印的字符串后添加不需要的 material。 print()
,但是没有:
[user@manjaro lua]$ lua
Lua 5.4.2 Copyright (C) 1994-2020 Lua.org, PUC-Rio
> io.write('hello world')
hello worldfile (0x7fcc979d4520)
> print('hello world')
hello world
当我在程序中使用 io.write()
时它也能正常工作:
--hello.lua
io.write('hello world\n')
print ('hello world')
输出:
[user@manjaro lua]$ lua hello.lua
hello world
hello world
我在戴尔台式机上使用 Manjaro Linux。谁能告诉我这是怎么回事?提前致谢。
编辑:也许我应该补充一点,不需要的 material 总是这样的:
file (0x7f346234d520)
它总是 'file' 后跟一个看起来像括号中的大十六进制数。确切的数字在一个 shell 会话中保持不变,但在不同的 shell 会话之间变化。
"file (0x7fcc979d4520)
"(或任何地址)是 io.write
调用的 return 值,带有隐式的 tostring
.
lua(1) 手册页说
In interactive mode,
lua
prompts the user, reads lines from the standard input, and executes them as they are read. If the line contains an expression or list of expressions, then the line is evaluated and the results are printed.
这里的问题是 io.write('hello world')
既可以是表达式也可以是语句。由于它是一个有效的表达式,解释器输出不需要的 return 值。
作为解决方法,请尝试添加分号:
> io.write('hello world\n');
hello world
虽然 Lua 通常不需要在每行末尾的每个语句都使用分号,但它确实允许使用分号。这里很重要,这意味着语法不能是表达式,只能是调用函数的语句。所以解释器不会输出 returned 值。