First I want to mention that this is my first program in Lua. I need to develop a Lua application, which reads a CSV file. The file consists of four columns and an unknown number of rows. Therefore I have to read till end of lines of file. In each row, the xyz coordinate for points stored. These coordinates are stored as double values. Now I have to copy the values form the csv file to a table (array in Lua). Later in the file I have to edit a Robot Program for an igm Robot. Therefore I need the table. Till now I have the fallowing code, but I'm not sure if this is the rigth way to start this:
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;
So first I execute an C++ program, which creates the csv file, but later I want to change the process, so that the c++ program is independent from the Lua script. Here this line is just for testing. After this line I want to read the data from the csv file to a table and print the table to the screen. So for me, I would just print the content of the file to the command line, so I could check if the script is working correct.
I never worked with Lua before, and the documentation is really hard to understand for me. So I would be grateful for any help you can give me.
Edit: I now worked with the post by user3204845 to update my code. To print the table to screen I used the print command. But in that way I just got 0069b568
. So my idea was to use a for
-loop. But that does not work. Can someone give me a hint how to access a entry in a table in Lua. Heres my code:
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;