Ecco qui il listato in Lua che elabora il file data.txt dove sistemerai i numeri e crea il file dataReformat.txt.
Puoi regolare a piacere il formato decimale, o scientifico dando alla funzione save() le stringhe di formattazione.
Verifica la correttezza dell’output!!!!
`– convert numeric data
— eseguire con lua o con texlua
— restituisce i numeri sulla riga
local function split(l)
t = {}
for w in string.gmatch(l, “[^%s]+”) do
t[#t+1] = tonumber(w)
end
return t
end
— carica i dati in una tabella
local function loadLines(filename)
local t = {}
for line in io.lines(filename) do
local n = split(line)
if #n > 0 then — elimino linee vuote
t[#t+1] = n
end
end
t.dimension = #t
return t
end
— traduce la tabella in terne di numeri
local function parse(t)
for x, row in ipairs(t) do
if #row > 3 then — calcolo della terna su due righe
newrow = {}
for i=2,#row do
k = 1
if row == 10 then
newrow[#newrow+1] = row[i-1]*10^t[x-1][k]
k = k + 1
else
newrow[#newrow+1] = row[i-1]
end
end
t[x-1] = nil
t[x] = newrow
end
end
end
— salva i dati
local function save(t, frmt1, frmt2, frmt3)
— tabs separator formatting string
f = string.format(“%s\t%s\t%s”, frmt1, frmt2, frmt3)
— reforming the table
reform = {}
for i=1, t.dimension do
if t then
local line = string.format(f, t[1], t[2], t[3])
reform[#reform + 1] = line
end
end
file = io.open(“dataReformat.txt”,”w”)
file:write(table.concat(reform, “\n”))
file:close()
end
— execution
filename = “data.txt”
rows = loadLines(filename)
parse(rows)
save(rows, “%0.6f”, “%0.8f”, “%0.8E”)
`
Se ci sono problemi sono qui.
R.