1. Sequence Building

I know I am asking a simple question, but I am spending too much time
testing this.  I need to read from a file, delimited by commas, and place
into sequence of sequences.

That is

field 1,field 2,field 3,field 4

would result in

line = {{field 1},{field 2},{field 3},{field 4}}


I am getting confused when I start with

line = {}

do I append, use &, or what ?  I am reading with getc, so I have to & some
and append some. But, I am not able to keep it straight.  Any clarification
available?

Should I start with

line = {{},{},{},{}}

It is much easier to process.  But ,this is so restrictive as to number of
fields to read! I am clear on the input procedure. The problem exists
whether I use gets or getc, I still have to parse the line.


( BTW, I won't stop using my signature based on the recent rash of flames.
Actually, the analogy is quite appropriate. If you want me off the list,
please indicate that as non-profanely as you can. )


    +
    |
   / \
  /   \  Rev. Joe Phillips, Pastor
  |SBC|  Mambrino Baptist Church   817-573-4901
  |   |  Granbury, Texas

new topic     » topic index » view message » categorize

2. Re: Sequence Building

At 10:01 AM 4/27/98 -0700,Joe Phillips wrote:

>I know I am asking a simple question, but I am spending too much time
>testing this.  I need to read from a file, delimited by commas, and place
>into sequence of sequences.
>
>That is
>
>field 1,field 2,field 3,field 4
>
>would result in
>
>line = {{field 1},{field 2},{field 3},{field 4}}
>
>
With a text file as follows:

Field 1,field 2,Field 3,field 4
field 5,field 6,Now is the time for all good men to come to the aid
of the party

And this code:
-- program treats end of line same as a comma
sequence s
object input
atom fn

fn = open("TEST.TXT","r")
s = {}
while 1 do
  input = gets(fn)
  if atom(input) then exit -- EOF
  end if
  if input[length(input)] = 10 then -- kill linefeed if necessary
     input = input[1..length(input)-1]
  end if
  input = input & ',' -- make sure there's an end to this sequence
  for i = 1 to length(input) do
    s = append(s,input[1..find(',',input)-1])       -- lose the comma
    input = input[find(',',input)+1..length(input)] -- clip off used part
    if length(input) < 1 then exit                       -- all done
    end if
  end for
end while

for i = 1 to length(s) do -- proof it works
 printf(1,"Field %d is: %s\n",{i,s[i]})
end for

This will parse the input nicely. I didn't use getc(), cause
in my opinion,  it's just one step above calling interrupts
for input. gets() does the job with less coding.

Irv

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

3. Re: Sequence Building

Joe Phillips wrote:
>
> I know I am asking a simple question, but I am spending too much time
> testing this.  I need to read from a file, delimited by commas, and place
> into sequence of sequences.
>
> That is
>
> field 1,field 2,field 3,field 4
>
> would result in
>
> line = {{field 1},{field 2},{field 3},{field 4}}
>
> I am getting confused when I start with
>
> line = {}
>
> do I append, use &, or what ?  I am reading with getc, so I have to & some
> and append some. But, I am not able to keep it straight.  Any clarification
> available?
>
> Should I start with
>
> line = {{},{},{},{}}
>
> It is much easier to process.  But ,this is so restrictive as to number of
> fields to read! I am clear on the input procedure. The problem exists
> whether I use gets or getc, I still have to parse the line.
>
> ( BTW, I won't stop using my signature based on the recent rash of flames.
> Actually, the analogy is quite appropriate. If you want me off the list,
> please indicate that as non-profanely as you can. )
>
>     +
>     |
>    / \
>   /   \  Rev. Joe Phillips, Pastor
>   |SBC|  Mambrino Baptist Church   817-573-4901
>   |   |  Granbury, Texas


hmmm, just off the top of my head:(gotta go to work in a few minutess
so this gonna be rough, it does seem to work though)

----------begin code snipet-----------------------

function read_file(sequence filename)
atom a,fn
sequence one,two

fn = open (filename,"r")--- open the file for reading
if fn = -1 then
printf(1,"cant open %s!",{filename})
abort(2)
end if

a=0
one={}
two={}
while a != -1 do --- want the whole file
        one={}
        while (a !=',') and (a!=-1) and (a!='\n')do --- look for our
delimiter,eof and \n
                a = getc(fn)
                one = one&a  --- add to current element
        end while
if a!=-1 then a = 0 end if
two = append(two,one)--- add element to full sequence
end while
return two
end function




-----------------------endcode snipet-----------------------
        this will should also treat \n as a delemiter (like the ',')


hope it helps.
                kasey

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

4. Re: Sequence Building

At 01:36 PM 4/27/98 -0500, kasey wrote:
>Joe Phillips wrote:
I need to read from a file, delimited by commas, and place
>> into sequence of sequences.
>> field 1,field 2,field 3,field 4
>> would result in
>> line = {{field 1},{field 2},{field 3},{field 4}}
>>
>hmmm, just off the top of my head:(gotta go to work in a few minutes
>so this gonna be rough, it does seem to work though)
>
>----------begin code snipet-----------------------
>
>function read_file(sequence filename)
>atom a,fn
>sequence one,two
>
>fn = open (filename,"r")--- open the file for reading
>if fn = -1 then
>printf(1,"cant open %s!",{filename})
>abort(2)
>end if
>
>a=0
>one={}
>two={}
>while a != -1 do --- want the whole file
>        one={}
>        while (a !=',') and (a!=-1) and (a!='\n')do --- look for our
>delimiter,eof and \n
>                a = getc(fn)
>                one = one&a  --- add to current element
>        end while
>if a!=-1 then a = 0 end if
>two = append(two,one)--- add element to full sequence
>end while
>return two
>end function
>
Not bad. Kasey's code is a bit shorter than mine. There are
two things that need fixing: It includes the commas, which
should be discarded, and the last element of the sequence
will have a -1 as the last "character". I don't think that does
any harm...?
Here's my attempt using Kasey's idea (fixes included)

function read_file(sequence filename)
filehandle fn
sequence one,two
atom a

fn = open (filename,"r")--- open the file for reading
one={}
two={}
a = 0
while a != -1 do --- want the whole file
        a = getc(fn)
        if (a = ',') or (a = '\n') or (a = -1) then
          two = append(two,one)
          one = {}
        else one = append(one,a)
        end if
end while
return two
end function

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

5. Re: Sequence Building

Irv Mullins wrote:
>

<snipage here>

> >
> Not bad. Kasey's code is a bit shorter than mine. There are

just luck there, I haven't been coding euphoria long enough
to fluent, and just 'see' better/smaller ways to do things
(not that I'm trying to compete or outdo anyone)

> two things that need fixing: It includes the commas, which
> should be discarded, and the last element of the sequence
> will have a -1 as the last "character". I don't think that does
> any harm...?
> Here's my attempt using Kasey's idea (fixes included)

        now leaving those in I have no excuse for, thanks for the fix.

> function read_file(sequence filename)
> filehandle fn
> sequence one,two
> atom a
>
> fn = open (filename,"r")--- open the file for reading
> one={}
> two={}
> a = 0
> while a != -1 do --- want the whole file
>         a = getc(fn)
>         if (a = ',') or (a = '\n') or (a = -1) then
>           two = append(two,one)
>           one = {}
>         else one = append(one,a)
>         end if
> end while
> return two
> end function


        I would imagine replacing the inner loop with the if-then makes it
faster also.
depends in part on how euphoria handles them (probably very well).


                        Kasey

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

Search



Quick Links

User menu

Not signed in.

Misc Menu