在Lua中将文件中的数字写入数组

时间:2016-11-28 00:13:25

标签: arrays lua

我有一个看起来如下的文件:

some text
184 3.7872  184 2.5076
185 3.7891  185 2.5063
186 3.7912  186 2.5042

我想跳过第一行并将数据写入数组。

到目前为止我得到了什么:

local file = io.open("test.txt", "r");
local readFile = {}
for line in file:lines() do
   table.insert (readFile, line);
end

--initialise target array
local array = {}
for i=2,# readFile,1 do
   array[i-1] = {}
   for j=1,4,1 do
      array[i][j] = 0
   end
end

for i=2,# readFile,1 do
   --split lines from readFile
   --write numbers into target array
end

似乎当我想初始化数组时会出现一些我不理解的错误:

lua: script.lua:13: attempt to index a nil value (field '?')
stack traceback:
    script.lua:13: in main chunk
    [C]: in ?

有人可以帮助我吗?另外,当我调用readFile [2]时,我得到184 3.7872 184 2.5076,我必须拆分并写入数组。我该怎么做?

2 个答案:

答案 0 :(得分:0)

以下是诀窍,但效率很低:

local file = io.open("test.txt", "r");
local readFile = {}
for line in file:lines() do
   table.insert (readFile, line);
end

--initialise target array
local array = {}
for i=1,(#readFile)-1,1 do
   array[i] = {}
   for j=1,4,1 do
      array[i][j] = 0
   end
end

function string:split( inSplitPattern, outResults )
   if not outResults then
      outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern,     theStart )
   while theSplitStart do
      table.insert( outResults, string.sub( self, theStart,    theSplitStart-1 ) )
      theStart = theSplitEnd + 1
      theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

local myTable = readFile[2]:split(" ")
for i = 1, #myTable do
   print( myTable[i] ) -- This will give your needed output
end

如果某人有更高效的方式,我也会感到高兴。

答案 1 :(得分:0)

无需过度复杂化。

local file = io.open("test.txt", "r");
local array = {}
for line in file:lines() do
    local t = {}
    for s in line:gmatch("%S+") do
        t[#t+1] = tonumber(s)
    end
    array[#array+1] = t
end

而且,对于我改变的内容,这里有一个不好的解释。

local file = io.open("test.txt", "r");
-- local readFile = {} Don't need this either.
local array = {} -- Init it here, cleaner and easier.
for line in file:lines() do
    local t = {} -- This is our line in the array.
    for s in line:gmatch("%S+") do -- Iterate through the string in blocks of non-whitespace. Check the Lua Patterns Matching Tutorial online.
        t[#t+1] = tonumber(s)
    end
    array[#array+1] = t

    --[[ If you want to just have the numbers, without caring about the newlines, just do

    for s in line:gmatch("%S+") do
        array[#array+1] = tonumber(s)
    end

    instead]]
end

-- Snipping that bottom part, we can do this much more efficiently.

一些提示,

  • Lua Arrays / Tables不需要初始化,它们是动态的。
  • 当您初始化数组[i-1]时,您的错误来自于尝试索引数组[i]。
  • for i=a, b, c do个循环中,c只需要指定1for i=1, #lines do所以#lines也可以。
  • #不是函数,它是索引(技术上)。如果你将它与它所计算的东西配对,它看起来更干净。 # lines代替//select the node using a javascript function getSelection()... var current_node = document.getSelection().anchorNode.parentElement; //node of current line's parent` //test the var console.log(typeof current_node); // IS javascript object console.log(current_node instanceof $); //IS NOT a JQuery object // by wrapping the var in $(), it can be operated on by JQuery functions, effects, etc... console.log($(current_node) instanceof $); IS a JQuery object $(current_node).fadeOut("slow");