1. Newbie needs help with text files

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...


Am I missing something?


Andy Serpa
renegade at earthling.net

new topic     » topic index » view message » categorize

2. Re: Newbie needs help with text files

------=_NextPart_000_0077_01BFCD7D.8137FF40
        charset="iso-8859-2"

> 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}


I have that one for you. Look at attached file. I have collected a couple of
my functions into this file taht will help you.

If you want that words are separated into sequence where commas are, it's
much simpler thing, I can write it for you if you want.

------=_NextPart_000_0077_01BFCD7D.8137FF40
        name="word.ex"
Content-Transfer-Encoding: quoted-printable

new topic     » goto parent     » topic index » view message » categorize

3. Re: Newbie needs help with text files

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...

new topic     » goto parent     » topic index » view message » categorize

4. Re: Newbie needs help with text files

Ok, thank you both --

I think I will be able to figure it out now...


Andy Serpa
renegade at earthling.net

new topic     » goto parent     » topic index » view message » categorize

5. Re: Newbie needs help with text files

On 3 Jun 2000, at 17:02, koda wrote:

>
> ------=_NextPart_000_0077_01BFCD7D.8137FF40
>         charset="iso-8859-2"
>
> > 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}
>
>
> I have that one for you. Look at attached file. I have collected a
> couple of my functions into this file taht will help you.
>
> If you want that words are separated into sequence where commas are,
> it's much simpler thing, I can write it for you if you want.

Yeas, just use parse(TheInputString,",") from the archives.

Kat

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu