【发布时间】:2016-10-21 08:33:18
【问题描述】:
首先我想提一下,这是我在 Lua 中的第一个程序。我需要开发一个读取 CSV 文件的 Lua 应用程序。该文件由四列和未知数量的行组成。因此我必须阅读到文件行尾。在每一行中,存储点的 xyz 坐标。这些坐标存储为双精度值。现在我必须将值从 csv 文件复制到一个表(Lua 中的数组)。稍后在文件中,我必须为 igm 机器人编辑机器人程序。因此我需要这张桌子。直到现在我有闲置的代码,但我不确定这是否是开始这个的正确方法:
local open = io.open
local function read_file(path)
local file = open(path, "r") -- r read mode and b binary mode
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
return content
end
os.execute("OpenGLM_3.exe -slicesize 3 -model cube_2.stl -ascii - eulerangles 0 0 0")
local fileContent = read_file("data.csv");
return 0;
所以首先我执行了一个 C++ 程序,它创建了 csv 文件,但后来我想改变这个过程,使 C++ 程序独立于 Lua 脚本。这里这条线只是为了测试。在这一行之后,我想将 csv 文件中的数据读取到表格中并将表格打印到屏幕上。所以对我来说,我只是将文件的内容打印到命令行,这样我就可以检查脚本是否工作正常。
我以前从未使用过 Lua,文档对我来说真的很难理解。因此,如果您能给予我任何帮助,我将不胜感激。
编辑:我现在使用user3204845 的帖子来更新我的代码。要将表格打印到屏幕上,我使用了 print 命令。但这样我就得到了0069b568。所以我的想法是使用for-loop。但这不起作用。有人可以给我一个提示如何访问 Lua 表中的条目吗?这是我的代码:
local open = io.open
local function read_file(path)
local file = open(path, "r") -- r read mode and b binary mode
if not file then return nil end
local coordinates = {}
for line in io.lines(path) do
local coordinate_x, coordinate_y, coordinate_z = line:match("%s*(.-),%s*(.-),%s*(.-)")
coordinates[#coordinates+1] = { coordinate_x = coordinate_x, coordinate_y = coordinate_y, coordinate_z = coordinate_z }
end
file:close()
return coordinates
end
os.execute("OpenGLM_3.exe -slicesize 3 -model cube_2.stl -ascii - eulerangles 0 0 0")
local coordinates = read_file("data.csv")
for line in coordinates
print(coordinates[line])
end
return 0;
【问题讨论】: