1. Newbie file/record handling Question #1
I've only just recently found Euphoria, and it looks very promising. My
first question concerns processing data from a file (and of course
handling objects/sequences).
Hypothetically: I have a file with a record that contains data as
follows:
FIELD1=aaaaa FIELD2=bbbbb FIELD4=dddddddddd FIELD5=ccc
I have
-------
integer infile
object inrec
infile=open("c:\\data\\infile.dat","r")
.
.
inrec=gets(infile)
.
.
--------
Now, I want to be able to parse inrec, Identifying the starting position
of FIELD1, FIELD2, etc so that I can get at the values associated with
each field identifier.
Is there a way to do this easily - my ignorance can only come up with
sequentially searching through the sequence for 7 byte slices matching
against various constants containing the individual field identifier
values.
As I said in the message header - complete newbie in Euphoria - - But
many years in IT.
Thanks in advance for the help - I'm still a tad bit confused on this
sequence thingie.
Dave E.
dedwards at technologist.com
2. Re: Newbie file/record handling Question #1
- Posted by David Cuny <dcuny at LANSET.COM>
Jun 23, 1999
-
Last edited Jun 24, 1999
Dave E. Edwards wrote:
>Hypothetically: I have a file with a record that contains data as
>follows:
>
>FIELD1=aaaaa FIELD2=bbbbb FIELD4=dddddddddd FIELD5=ccc
From your description, each record takes the form:
<record> = <fields>
<fields> = <field> [ <space> <fields> ]
<field> = <name> '=' <value>
From your description, it would seem that each field is delimited by a
single space. So a routine that would return a list of fields would be:
function getFields( sequence record )
-- return a sequence of space delimited fields
integer spaceAt
-- look for a space
spaceAt = find( ' ', record )
-- no space?
if spaceAt = 0 then
-- this is the last field
return {record}
else
-- get the record, and recursively get the rest
return {record[1..spaceAt-1]} &
etFields( record[spaceAt+1..length(record)] )
end if
end function
Knowing when to use & and append(), and where to put {} around things can be
confusing - I still had to test my code out to make sure I was getting the
right results. So input of:
getFields( ''123 456 789'' )
will return:
{ ''123'', ''456'', ''789'' }
Please excuse the use of two single quotes instead of a real quote mark -
the quote key on this keyboard is broken. Now, the probem is to split the
records into names and values. Good thing there's an '=' to look for:
function splitField( sequence field )
integer equalAt
equalAt = find( '=', field )
return { field[1..equalAt-1], field[equalAt+1..length(field)] }
end function
So:
splitField( ''123=abc'' )
will produce:
{ ''123'', ''abc'' }
Here's a function that puts it all together:
function parseLine( sequence line )
sequence out
-- parse line into field names and values
-- parse the line into fields
out = getFields( line )
-- convert each field into the name and data
for i = 1 to length( out ) do
-- replace field with split elements
out[ i ] = splitField( out[ i ] )
end for
return out
end function
Did I answer the right question?
-- David Cuny
3. Re: Newbie file/record handling Question #1
David Cuny wrote:
>
> Dave E. Edwards wrote:
>
> >Hypothetically: I have a file with a record that contains data as
> >follows:
> >
> >FIELD1=aaaaa FIELD2=bbbbb FIELD4=dddddddddd FIELD5=ccc
>
> >From your description, each record takes the form:
>
> <record> = <fields>
> <fields> = <field> [ <space> <fields> ]
> <field> = <name> '=' <value>
<SNIP>
> Did I answer the right question?
>
> -- David Cuny
Yes, David, you did - and thank you - - a great help, and a good insight
to using sequences.
Thanks again.
Dave E.
dedwards at technologist.com