使用LUA读取和解析文件
我试图让它从一个文件中读取x y z坐标到一个3d数组中。但它似乎不起作用 文件与使用LUA读取和解析文件,lua,Lua,我试图让它从一个文件中读取x y z坐标到一个3d数组中。但它似乎不起作用 文件与.lua脚本位于同一文件夹中 -9649.481 666.4141 117.3444 -9475.624 563.4871 116.0533 -9338.459 432.295 137.4043 请尝试以下功能: 函数readwaypoints(文件名,numberofwaypoints) 本地文件=io.open(文件名) 本地航路点={} 对于n=1,路点数量为 局部x,y,z x=文件:已读('*n')
.lua
脚本位于同一文件夹中
-9649.481 666.4141 117.3444
-9475.624 563.4871 116.0533
-9338.459 432.295 137.4043
请尝试以下功能:
函数readwaypoints(文件名,numberofwaypoints)
本地文件=io.open(文件名)
本地航路点={}
对于n=1,路点数量为
局部x,y,z
x=文件:已读('*n')
y=文件:已读('*n')
z=文件:已读('*n')
航路点[#航路点+1]={['x']=x,['y']=y,['z']=z}
结束
文件:close()
返回航路点
结束
它接受文件名和文件中的行数。对于示例文件,它应该返回如下表:
{[1]={x=-9649.481,y=666.4141,z=117.3444},
[2] ={x=-9475.624,y=563.4871,z=116.0533},
[3] ={x=-9338.459,y=432.295,z=137.4043}
那么会发生什么?你期望得到什么?你实际得到什么?你自己试过调试什么?你的程序可以优化很多。
function lines_from(file)
if not file_exists(file) then return {} end
for line in io.lines(file) do
tokens = {};
itr = 1;
for token in string.gmatch(line, "[^%s]+") do
tokens[ itr ] = token;
itr = itr + 1;
end
x = tokens[1];
y = tokens[2];
z = tokens[3];
g_lines_from[g_lines_fromCount] = { x, y, z };
g_lines_fromCount = g_lines_fromCount + 1;
end
end
function AddAll()
for i = 1, g_lines_from, 1 do
x, y, z = g_lines_from[i];
ListBoxEntry.Create( g_lbWaypoints, "X: " .. math.floor( x ) .. ", Y: " .. math.floor( y ) .. ", Z: " .. math.floor( z ) );
end
end
function OnAddWaypointClicked( eventID, button )
local file = "mine1-75.txt";
lines_from(file);
AddAll();
end;