我正在尝试在 lua 中打印以下内容作为输出。
inertia_x = {
{46.774, 0., 0.},
{0., 8.597, 0.},
{0., 0., 50.082}
}
x = {mass = 933.0, com = {0.0, 143.52, 0.0}, inertia_x}
print(x)
此代码是在文本编辑器中编写的,名为 sample.lua
现在我正在使用 linux 并在存储 .lua 文件时转到正确的目录并调用
$ lua 示例.lua
输出是表:0x55c9fb81e190
理想情况下,我希望像列表一样打印 x
这是我继 Hello World 之后的第二个 lua 程序。对 Linux 和编程也很陌生。
我将不胜感激您的帮助!
最佳答案
您需要检测表并递归构建表转储。试试这个 :
local inertia_x = {
{46.774, 0., 0.},
{0., 8.597, 0.},
{0., 0., 50.082}
}
local x = {mass = 933.0, com = {0.0, 143.52, 0.0}, inertia_x}
local function dump ( value , call_indent)
if not call_indent then
call_indent = ""
end
local indent = call_indent .. " "
local output = ""
if type(value) == "table" then
output = output .. "{"
local first = true
for inner_key, inner_value in pairs ( value ) do
if not first then
output = output .. ", "
else
first = false
end
output = output .. "\n" .. indent
output = output .. inner_key .. " = " .. dump ( inner_value, indent )
end
output = output .. "\n" .. call_indent .. "}"
elseif type (value) == "userdata" then
output = "userdata"
else
output = value
end
return output
end
print ( "x = " .. dump(x) )
关于linux - 尝试在 lua 中打印表格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55650773/