Re: Newbie Question

new topic     » goto parent     » topic index » view thread      » older message » newer message

First, scan the Euphoria archive at www.rapideuphoria.com.  Someone may 
have already written a good routine for reading in a comma-delimited file.

Yes, this is something Euphoria would handle very well.  I've found it 
handles pretty much anything I need to do very well.

The program would go something like this (just typed in, untested, but 
should be pretty close).  Probably needs some more catches for bad data, 
etc.  This will be fine with data like
hello,this is the data,do,you like,it,45,98454,-89
but will NOT properly read data with extra commas, such as:
"Here is a readable sentence","The comma here, will hose things 
up",43,56,end

You can work on it properly reading that kind of stuff, but this should get 
you started.

Good luck!
Ted

BEGIN PROGRAM-----------------------------------------
include file.e   -- Standard Euphoria include file for file i/o routines

function readthefile(sequence thefile)
    object line     -- always declare file input buffers as objects
    integer fn,i
    sequence thedata,arecord
    thedata={}
    arecord={}
    fn=open(thefile,"r")     -- (or "rb" for binary files)
    if fn=-1 then
        return {}  -- file could not be opened  return empty data
    end if
    line=gets(fn)   -- read a line from the file
    while not atom(line) do  -- -1 (an atom) is returned at EOF
        line=line[1..length(line)-1]  -- chop off trailing '\n' character
        i=match(",",line)   -- locate the first comma
        -- this is to catch lines with one element but no commas
        if i=0 then
            arecord=line
        end if
        while i>0 do   -- i=index location of first comma, will be 0 when 
not found
            arecord=append(arecord,line[1..i-1])
            -- this if/then is to prevent incrementing i past length of line
            if i+1 <= length(line) then
                line=line[i+1..length(line)]
            else
                exit
            end if
            i=match(",",line)
            -- this if/then should prevent an infinite loop if a line
            -- ends in a comma
            if i=1 and length(line)=1 then
                exit   -- exit will throw you out of the current for or 
while loop
            end if
        end while
        -- don't bother adding empty records
        if length(arecord)>0 then
            thedata=append(thedata,arecord)
        end if
        arecord={}      -- reset record data to nothing
        line=gets(fn)   -- continue on
    end while
    close(fn) -- always close files you've opened
    return thedata
end function

sequence filedata,thefile
thefile="c:\\somedata.dat"
mydata = readthefile(thefile)
-- whatever you want to do with the data goes here

END PROGRAM-----------------------------------------


--On Wednesday, April 25, 2001 11:12 AM -0400 tpopel at yahoo.com wrote:

>
>
>
>
> I have to do some work with 2 dimensional, comma
> delimited text files.
>
> Is this the kind of thing Euphoria does well?
>
> What replaces the old input# statement from basic?
>
>
>
>
>
>
>

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu