2012-10-15 4 views
2

파일에서 xyz 좌표를 3D 배열로 읽으려고합니다. 그러나 그것은 작동하지 않는 것 같습니다. 그것은 파일 이름과 라인 수에 소요LUA로 파일 읽기 및 구문 분석

function readwaypoints(filename, numberofwaypoints) 
    local file = io.open(filename) 
    local waypoints = {} 
    for n = 1, numberofwaypoints do 
    local x, y, z 
    x = file:read('*n') 
    y = file:read('*n') 
    z = file:read('*n') 
    waypoints[#waypoints+1] = {['x'] = x, ['y'] = y, ['z'] = z} 
    end 
    file:close() 
    return waypoints 
end 

:

파일은

-9649.481 666.4141 117.3444 
-9475.624 563.4871 116.0533 
-9338.459 432.295 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; 
+1

그래서 어떻게됩니까? 무엇을 얻을 것으로 기대합니까? 실제로 무엇을 얻으십니까? 당신이 직접 디버깅을 시도한 적이 있습니까? –

+0

프로그램을 ** LOT **로 최적화 할 수 있습니다. – hjpotter92

답변

1

는 다음과 같은 기능을 시도해보십시오 .lua 스크립트로 같은 폴더에 있습니다 파일. 예제 파일의 경우 다음과 같은 테이블을 반환해야합니다.

{[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}}