Re: Notazione numerica in Mathematica

#86203
robitex
Amministratore del forum
    Up
    0
    Down
    ::


    Ecco qua:
    `– convert numeric data
    — eseguire con lua o con texlua

    — restituisce i numeri sulla riga
    local function split(l)
    local t = {}
    for w in string.gmatch(l, “[^%s]+”) do
    local m = tonumber(w)
    if m then — dato numerico
    t[#t+1] = {real = m}
    else — dato non numerico
    t[#t+1] = {text = w}
    end
    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 — mantenere!!! linee vuote
    t[#t+1] = n
    else
    t[#t+1] = {}
    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
    local newrow = {}
    local start = 2
    if row[1].text then
    newrow[#newrow + 1] = row[1].text
    start = start + 1
    end
    for i=start,#row do
    local k = 1
    if row.real and row[i-1].real then
    local m = row.real
    if m == 10 then
    newrow[#newrow+1] =
    {real = row[i-1].real*10^t[x-1][k].real}
    k = k + 1
    else
    newrow[#newrow+1] = {real = m}
    end
    else
    newrow[#newrow+1] = {text = row.text}
    end
    end
    t[x-1] = nil
    t[x] = newrow
    end
    end
    end

    — aggiustare i formati delle colonne qui
    local format = {“%0.6f”, “%0.8f”, “%0.8E”}

    — salva i dati
    local function save(t)
    — tabs separator formatting string
    — reforming the table
    reform = {}
    for i=1, t.dimension do
    if t then
    if #t == 0 then — empty line
    reform[#reform + 1] = “”
    else
    line = {}
    for pos=1,3 do
    if t[pos].real then
    line[#line+1] = string.format(format[pos],t[pos].real)
    else
    line[#line+1] = t[pos].text
    end
    end
    reform[#reform + 1] = table.concat(line, “\t”)
    end
    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)`

    Probabile che ripensando all’algoritmo si trovi una soluzione più elegante, ma questa ha il pregio di funzionare (almeno spero) e di gestire il formato d’ingresso per tutte e tre le colonne.
    Ed ora buona notte!
    R.

    Go to top