read_text.lua


NAME
    read_text

FUNCTION
    read_text(fname, dm)

NOTES
    Reads data from a text file.
    A comment line is expected to start with #.
    The first record of record must contain the numners of rows and columns.

INPUTS
    fname - file name
    dm    - delimiter to separate data

OUTPUTS
    a zeArray object

SOURCE

require("register")

function read_text(fname, dm)
    local arr = zeUtl.new("double")
    local k = 0
    local row = 0
    local col = 0
    local pattern = "%d" .. dm
    for line in io.lines(fname) do
        if (string.sub(line, 1, 1) ~= "#") then
            if (row == 0 or col == 0) then
                local i, j = string.find (line, pattern , 1)
                if (j) then
                    row = tonumber(string.sub(line, 1, j-1))
                    col = tonumber(string.sub(line, j+1, string.len(line)))
                    if (row > 1 and col > 1) then
                        arr:resize(row, col)
                    else
                        error("Invalid row/column specification.")
                    end
                end
            else
                i, j = string.find (line, "%d", 1)
                if (i) then
                    arr:parse(k, line)
                    k = k + 1
                end
            end
        end                
    end
    
    return arr
end