Lua Script CSV file to table -
i'm pretty new lua, have troubles reading data csv file table. csv file consist of 4 columns. first column string other 3 double values. want is: open file, read in data , process data. testing want print data screen. later have open other file, robot programm, , pass data programm.
i execute script consol command lua script.lua
. error message lua: script.lua:22: bad argument #1 ´format´ (number expected, got nil)
stack traceback: [c]: in function ´string.format´ script.lua:22: in main chunk [c]: in?
can tell me i'm doing wrong?
edit: changed scritp little bit. new code
local open = io.open local function read_file(path) local file = open(path, "r") -- r read mode , b binary mode --if not file return nil end local coordinates = {} line in io.lines(path) local coordinate_name, coordinate_x, coordinate_y, coordinate_z = line:match("%s*(.-),%s*(.-),%s*(.-),%s*(.-)") coordinates[#coordinates+1] = { coordinate_name=coordinate_name, coordinate_x = tonumber(coordinate_x), coordinate_y = tonumber(coordinate_y), coordinate_z = tonumber(coordinate_z) } end --file:close() return coordinates end local coordinates = read_file("data.csv") _, coordinate in ipairs(coordinates) -- use pairs or ipairs iterate on tables print(("x: %s, y: %s, z: %s"):format(coordinate.coordinate_x, coordinate.coordinate_y, coordinate.coordinate_z)) end return 0;
now can execute script, printed screen is: x: nil, y: nil, z: nil
. far understand lua, nil
means no values read table.
edit: file want read looks this:
after;-5;-5;0; after;-2;-5;0; after;5;-5;0; after;5;-2;0; after;5;5;0; after;2;5;0; after;-5;5;0; after;-5;2;0; after;-5;-5;0; intersects;5;-4;0 intersects;-5;-4;0 intersects;-5;-3;0 intersects;5;-3;0 intersects;5;-2;0 intersects;-5;-2;0
edit: updated code:
local open = io.open local function read_file(path) local file = open(path, "r") -- r read mode , b binary mode --if not file return nil end local coordinates = {} line in io.lines(path) local coordinate_name, coordinate_x, coordinate_y, coordinate_z = line:match("%s* (.*);%s*(.*);%s*(.*);%s*(.*);%s*(.*)") coordinates[#coordinates+1] = { coordinate_name = coordinate_name, coordinate_x = tonumber(coordinate_x), coordinate_y = tonumber(coordinate_y), coordinate_z = tonumber(coordinate_z) } print(("x: %s y: %4f z: %s"):format(coordinates.coordinate_x, coordinates.coordinate_y, coordinates.coordinate_z)) end _, coordinate in ipairs(coordinates) print(coordinates.coordinate_x, coordinates.coordinate_z, coordinates.coordinate_z) end file:close() return coordinates end local coordinates = read_file("data.csv") _, coordinates in ipairs(coordinates) -- use pairs or ipairs iterate on tables print(("x: %s, y: %s, z: %s"):format(coordinates.coordinate_x, coordinates.coordinate_y, coordinates.coordinate_z)) end return 0;
i'm using lua 5.3.3 windows, script writen in luaedit , called line lua script.lua
.
use (.*)
in pattern instead of (.-)
. per docs, -
match shortest pattern, seems bug out z coordinate in testing:
updated match file format posted
local coordinate_name, coordinate_x, coordinate_y, coordinate_z = line:match("([^;]*);([^;]*);([^;]*);([^;]*)")
the tonumber
handle space trimming , don't seem using coordinate_name anywhere. there plenty of string trim implementations can choose if need trim coordinate_name variable later on.
full script reference.
local open = io.open local function read_file(path) local file = open(path, "r") -- r read mode , b binary mode --if not file return nil end local coordinates = {} line in io.lines(path) local coordinate_name, coordinate_x, coordinate_y, coordinate_z = line:match("([^;]*);([^;]*);([^;]*);([^;]*)") coordinates[#coordinates+1] = { coordinate_name = coordinate_name, coordinate_x = tonumber(coordinate_x), coordinate_y = tonumber(coordinate_y), coordinate_z = tonumber(coordinate_z) } end file:close() return coordinates end local coordinates = read_file("data.csv") _, coordinate in ipairs(coordinates) -- use pairs or ipairs iterate on tables print(("x: %s, y: %s, z: %s"):format(coordinate.coordinate_x, coordinate.coordinate_y, coordinate.coordinate_z)) end return 0;
Comments
Post a Comment