-- -- CSVファイルを読み出し、テーブルを表示する -- --グローバル変数宣言---------------------- Path = ".\\" --luaファイルを保存しているPath(相対 or 絶対) Fname = "luacsv.csv" Xtitle = "" Ytitle = "" Value ={ X={}, Y={}, Z={} } ------------------------------------------ --文字の分解 ------------------------------------------ function split(str, d) local s = str local t = {} local p = "%s*(.-)%s*"..d.."%s*" local f = function(v) table.insert(t, v) end if s ~= nil then string.gsub(s, p, f) -- 最後の要素より前の要素を処理する f(string.gsub(s, p, "")) -- 最後の要素はカンマで区切られていないときにマッチしないので、その前の値を空白で置き換えてからマッチさせる end return t end ------------------------------------------ --CSVファイルを読み込みます ------------------------------------------ function readdata( filename ) local fp local msg local str local t={} local i = 1 --ファイルを開きます fp,msg = io.open( filename, "r") if( not(fp) )then print( "Can't open "..Path..Fname) return end --CSVデータを読み込みます while(true)do str = fp:read("*l") --1行読み込み if( str==nil )then break end --読込むデータが無ければ終了 str = string.gsub( str,"\r","" ) --改行コードを外す t = split( str, "," ) if( t[1]=="xtitle" )then Xtitle = t[2] end if( t[1]=="ytitle" )then Ytitle = t[2] end if( t[1]=="ztitle" )then Ztitle = t[2] end if( t[1]~="xtitle" and t[1]~="ytitle" and t[1]~="ztitle" )then Value.X[i] = t[1] Value.Y[i] = t[2] Value.Z[i] = t[3] print("["..i.."]".." "..Value.X[i].." "..Value.Y[i].." "..Value.Z[i]) i = i + 1 end end io.close(fp) end ------------------------------------------ --メインプログラム ------------------------------------------ function main() --CSVファイルを読み込みます readdata( Path..Fname ) end main()