Re: Sequence Building

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

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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu