lua-users home
lua-l archive

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


that is a very good point! and something i will use...

but i would also like to know how to parse floating point using lex as my
data files will not always be in the same format as my Lua tables (for
readability and external user editing purposes)

-----Original Message-----
From: Luiz Henrique de Figueiredo [mailto:lhf@tecgraf.puc-rio.br]
Sent: 28 May 2003 15:16
To: lua@bazar2.conectiva.com.br
Subject: Re: lex parsing floating point values


>what is the best way to parse floating point values from an external file?
>
>Particles = 100
>Emitter.delay = 0.001
>Emitter.delayFuzz = 0

Why parse this by hand at all? This file is valid Lua file: let Lua do
the work for you with lua_dofile. You'll have to create Emitter first.
Something like this (in Lua 5.0, but similar code works for Lua 4.0):

 local particle = {Emitter={}}
 local f=assert(loadfile("yourfile"))
 setfenv(f,particle)
 f()
 print(particle.Particles)
 print(particle.Emitter.delay)
 print(particle.Emitter.delayFuzz)

If you don't mind polutting the global space then dofile("yourfile")
suffices.
--lhf