Question

Does someone know howto parse scientific numbers from a textfile with lua?

example textfile:

0.2 0.5 0.15 5.32E-05 0.5

0.2 6.32E-08 0.5

I found in a related topic howto get the numbers (below), but with that code it sees '5.32E-05' as: 5.32 and -0.5.

local tt = {}
for line in io.lines(filename) do
local t = {}
for num in line:gmatch'[-.%d]+' do
  table.insert(t, tonumber(num))
end
if #t > 0 then
  table.insert(tt, t)
end
end

can someone help me?

Was it helpful?

Solution

You can use the *n read pattern if you're sure you're reading numbers.

If you read the file line by line as in your example code, then extract the words in the line using %S+ and convert them to numbers with tonumber.

Bottom line: leave the heavy parsing to tonumber.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top