lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Ashwin Hirschi wrote:

> Mind you, the first scenario would still need to be extended to process
> the pending data in Lines, after the while loop.

In my case the last empty line is always there but you are right I
should use a more robust and flexible way so I enhanced the nil test:

   local Lines={}
   while true do
      local Line=Handle:read('*line')
      if Line==nil or Line=='' then
--       process lines here; e.g. with iterator
         Lines={}
         if Line==nil then break end
      else
         table.insert(Lines,Line)
      end
   end

But this can be done better so I will end up here. Also I added the
carriage-return strip.

   local Lines={}
   repeat
      local Line=Handle:read('*line')
      Line=Line and string.gsub(Line,'\r','')
      if Line==nil or Line=='' then
--       process lines here; e.g. with iterator
         Lines={}
      else
         table.insert(Lines,Line)
      end
   until Line==nil


> Jamie Webb wrote:

> The first, clearly. The second is needless obfuscation, and would run
> slower. In fact, as written, it won't work at all. The declaration of
> Lines needs to be moved outside the loop.

Thank you for the hint. I have seen this bug a few minutes after posting
the code. Conclusion: Extracting the basic concept from a bigger code
block is often the best way to find bugs and tune up the code.

--
Markus