2011-01-18 41 views
0

我全新到Lua從單純的VB.Net背景。我有一個需要迭代的maplines大文件。的數據是在以下形式:你如何解析字符串在Lua「1 2 3 4」?

; XX.XXX YY.YYY名稱 ; [航空公司] 58.50 -12.44 58.21 -13.73

58.21 -13.73 57.89 -15.02

57.89 -15.02 57.54 -16.30

57.54 -16.30 57.17 -17.58

57.17 -17.58 56.76 - 18.84

56.76 -18.84 56.33 -20.10

56.33 -20.10 55.87 -21.35

54.33 -25.02 53.77 -26.22

我試過這個,但不斷收到錯誤。

local mapLines = {} 

local filePath = system.pathForFile("data.ini", system.DocumentsDirectory) 

local file = io.open(filePath, "r") 

if file then 
local contents = file:read("*a") 

--print("Contents of " .. filePath) 
--print(contents) 

io.close(file) 

local t = display.newText("Contents of ", 5, 80, nil, 16); 
t:setTextColor(255, 255, 136, 255); 
local t = display.newText(filePath, 5, 100, nil, 10); 
t:setTextColor(255, 255, 136, 255); 

local ylast = 130 
for line in io.lines(filePath) do 
    local t = display.newText(line, 15, ylast, nil, 14); 
    t:setTextColor(255, 255, 255);  
    ylast = ylast + 20 

    n = tonumber(line); 
    if n == nil then 

     local f = {} 
     s = "1 2 3 4" 
     for k, v in string.gmatch(s, "(%w+) (%w+)") do 
     f[k] = v 
      end 



local myLine = newLine(tonumber(f[1]), tonumber(f[2]), tonumber(f[3]), tonumber(f[4])) 
     table.insert(mapLines, myLine) 
    end 
    end 
end 


-- Example of shape drawing func 


local function newLine(x,y,x1,y1) 

-- need initial segment to start 
local Line = display.newLine(x, y, x1, y1) 

Line:setColor(30, 155, 30, 100) 
Line.width = 3 

return Line 
end 

Runtime:addEventListener("enterFrame", mapLines) 

任何幫助將不勝感激!

戴夫

+1

請修正格式

local string_to_parse = '1 2 3 4' for s in string_to_parse:gmatch('%d+') do print(s) end 

示例代碼。請說明您獲得的錯誤的詳細信息,或者生成一個在Lua控制檯中工作的最小示例。你的代碼顯然是一些Lua腳本框架。 – 2011-01-19 06:01:32

+0

我已經大幅重新格式化了代碼。我做的一個關鍵是用空格替換所有標籤,因爲標籤字符被降價引擎誤解。我也壓縮了大部分空白行。如果我太過於苛刻,請隨時放回一些。 – RBerteig 2011-01-19 10:21:59

回答

4

回答這個問題的主題:在codepad