Re: Newbie needs help with text files
- Posted by Irv Mullins <irv at ELLIJAY.COM> Jun 03, 2000
- 474 views
Hi Andy: If your data file consists of only numbers: 12.5, 890, 7.23, 84.3 for example, you can read this in with the following code: include get.e -- for the get() function integer fn -- file handle sequence s -- storage for the completed sequence object input -- storage for single numbers fn = open("test.dat","r") -- open the data file s = {} -- initialize the empty sequence while 1 do input = get(fn) -- read a number if input[1] = GET_SUCCESS then s &= input[2] -- append else exit -- or EOF end if end while ? s -- for debugging If you want to read in strings mixed with the numbers, it's a bit more complicated. You will need a routine to read a series of characters into a string variable until the comma is encountered: include get.e include file.e integer fn sequence s object input function read_string() object temp, char char = seek(fn,where(fn)-1) -- rewind the file by 1 char temp = "" while 1 do char = getc(fn) -- read characters in if char = ',' -- until comma is found or char = -1 then return {temp} -- or EOF else temp &= char -- tack on the character end if end while end function fn = open("test.dat","r") s = {} while 1 do input = get(fn) if input[1] = GET_SUCCESS then s &= input[2] -- tack on the number elsif input[1] = GET_FAIL then s &= read_string() -- or the string else exit -- anything else is EOF end if end while ? s puts(1,s[4]) -- display the 4th item (ur name) Hope this helps: Irv On Sat, 03 Jun 2000, Andy wrote: > Hello, > > I am new to Euphoria, and couldn't find the answer to this elsewhere -- > maybe you can help me. > > Is there a simple way to take a plain text data file, say a comma-delimted > file with a bunch of numbers in it (and a few words) and turn those numbers > into Euphoria atoms or sequences so I can do math on them? > In other words, can I take a text file that looks like this: > > 12.5, 890, 7.23, Andy Serpa, 84.3 > > and turn it into a sequence like this: > > {12.5, 890, 7.23, "Andy Serpa", 84.3} > > > instead of a sequence like this: > {49,50,46,53,44,32,56,57,48,44,32,55, etc....} > > > or simply be able to read in any number from a text file and have it remain > a single number instead of a sequence of bytes representing the characters > of the number...