1. Newbie Question
- Posted by tpopel at yahoo.com
Apr 25, 2001
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?
2. Re: Newbie Question
Howdy!
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?
Very well, for numerous reasons!
> What replaces the old input# statement from basic?
Wow, that was a *long* time ago. Somebody back me up on this, but I
believe that you are looking for the Euphoria function gets(). So,
you'd do something like this ...
integer
FileId
object
data
FileId = open("mydata.txt", "r")
data = gets(FileId)
...
close(FileId)
-- Travis --
3. Re: Newbie Question
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?
>
>
>
>
>
>
>
4. Re: Newbie Question
Of course immediately after I sent it, I spotted bug #1.
Replace this block:
> 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
With this block:
> if i=0 then
> arecord=line
> else
> 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
> end if
--On Wednesday, April 25, 2001 1:44 PM -0500 Ted Fines
<fines at macalester.edu> wrote:
> 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)
<snip>
5. Re: Newbie Question
----- Original Message -----
From: <tpopel at yahoo.com>
> I have to do some work with 2 dimensional, comma
> delimited text files.
>
> Is this the kind of thing Euphoria does well?
Yes - you'll probably want the strtok.e include file from the RDS website ,
it makes parsing much easier.
> What replaces the old input# statement from basic?
gets(fn) -- "gets" a string from fn where fn is the assigned file handle.
You might try something like:
include strtok.e
object data, line
atom fn
data = {}
line = ""
fn = open("myfile","r") -- may want to check fn = -1, means can't open file
while 1 do
line = gets(fn)
if atom(line) then exit -- EOF
else
line = parse(line,',') -- line in = "one,two,three", out =
{"one","two","three"}
data = append(data,line) -- store it away for later use
end if
end while
Regards,
Irv
6. Re: Newbie Question
- Posted by jjnick at cvn.com
Apr 25, 2001
----- Original Message -----
From: <tpopel at yahoo.com>
To: "EUforum" <EUforum at topica.com>
Subject: Newbie Question
>
> 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?
>
Not to diminish Euphoria's capabilities, but "awk" is designed specifically
for this task . . .
7. Re: Newbie Question
----- Original Message -----
From: <rforno at tutopia.com>
> This does not solve the problem. gets() only gets one line (up to \n) from
> the file, but does not separate the items as delimited by commas.
True.
> After that, you should use the value() function.
Not yet..
Value is only for numbers. To separate a comma delimited line of
text, use the parse function in strtok.e. Once the line is separated into
individual sequences, then value() can be used to convert any numbers
found there.
Regards,
Irv