lua-users home
lua-l archive

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


what is the best way to parse floating point values from an external file?

you will have to forgive me, i am new to Lua and i am not familiar with lex
either

my test data consists of an integer and 2 float values:

Particles = 100
Emitter.delay = 0.001
Emitter.delayFuzz = 0

i would like to read in floats that could be in various formats:
e.g.
1
1.0
.01
-1
-.01
-1.68587e-007

this is the Lua code i'm trying to use to parse the file, but as i say i
don't know lex well enough:

readfrom( 'export.txt' )
file = read( "*a" )

particle = {}
particle.emitter = {}

i = 1
i, j = strfind( file, "Particles =" )
i, j, particle.particles = strfind( file, "(%d+)", j + 1 )
dprintf( '%i\n', particle.particles )

i, j = strfind( file, "Emitter.delay =", j + 1 )
i, j, particle.emitter.delay = strfind( file, "(%d%.%e)", j + 1 )
dprintf( '%i %i\n', i, j )
dprintf( '%f\n', particle.emitter.delay )

i, j = strfind( file, "Emitter.delayFuzz =", j + 1 )
i, j, particle.emitter.delayFuzz = strfind( file, "([0-9.e])", j + 1 )
dprintf( '%f\n', particle.emitter.delayFuzz )

readfrom()

hope someone can help me, thanks