lua-users home
lua-l archive

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


Have you looked into io.lines?

http://www.lua.org/manual/5.1/manual.html#pdf-io.lines

for line in io.lines('myfile.txt') do
  -- do something with the line
end

Also, you're using the for loop wrong.  For iterating over each line in
a string, you'd probably use string.gmatch; for a table, try ipairs or
pairs.

-Rob

On Mon, 18 Apr 2011 18:55:41 +0200
Gilles Ganault <gilles.ganault@free.fr> wrote:

> Hello
> 
> 	I'd like to read a text file into either a string or a a
> table, provided I can iterate through the items it contains.
> 
> Both ways end with error "unexpected symbol near 'for'":
> 
> ==============
> libs = io.open("dynlibs.txt","r")
> libs = libs:read("*all")
> -- unexpected symbol near 'for'
> for lib in libs do
> 	print(lib)
> end
> libs:close()
> ==============
> libs = io.open("dynlibs.txt","r")
> local tlibs = {}
> tlibs=libs:read("*all")
> -- unexpected symbol near 'for'
> for lib in tlibs do
>         print(lib)
> end
> ==============
> 
> How do this in Lua?
> 
> Thank you.
> 
> 

Attachment: signature.asc
Description: PGP signature