1. Re; Reading files
I am brand new at Euphoria. I haved programmed in other languages but
have decided I would give EUPHORIA a try.
I am trying to duplicate a program I wrote in QBASIC and my first
question is how do I describe a file where the first record has format 1
and all of the other records have format 2.
Also, the fields within each record have no field separators such as
commas or spaces.
Also, if I don't describe each file how do I break the record into
individual fields after I have read the record. After glancing thru the
manual there are alot of questions I have. I don't find any statements
where a long string can be separated. I am sure itg is there, I just
haven't found it yet or don't understand.
Thanks,
Jim Roberts
2. Re: Re; Reading files
- Posted by Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL>
Oct 27, 1998
-
Last edited Oct 28, 1998
>I am brand new at Euphoria. I haved programmed in other languages but
>have decided I would give EUPHORIA a try.
Welcome.
>I am trying to duplicate a program I wrote in QBASIC and my first
>question is how do I describe a file where the first record has format 1
>and all of the other records have format 2.
Euphoria does not support records in the way that QBasic natively supports
them, however a complete database engine is already available as a library
for example.
But to answer your question, here are two possible solutions:
1) The record you want to read, has a known length:
function get_record (integer fh, integer len)
sequence ret
ret = {}
for index = 1 to len do
ret = append(ret, getc(fh))
if ret[length(ret)] = -1 then
return ret
end if
end for
return ret
end function
-- Open the file:
integer fhandle
fhandle = open ("myfile.dat", "rb")
if fhandle = -1 then
puts (1, "Error opening file!\n")
abort (1)
end if
-- Read in record of assumes length 10 (you can change it off course):
sequence record
record = get_record (10)
You could 'cut' your record into pieces by just calling get_record with the
lenghts of each inidivdual item of the record, or you cut could it like
this:
I am showing you this, because in your message, you said you could not find
a routine to seperate a long string.
In Euphoria strings do not exist. They are like one dimensional arrays
containing values, those values are ASCII values. They all represent a
character.
'a' = 65 for example.
However, you could just use 'a'.
If in Euphoria you say:
text = "Euphoria"
You could have said:
text = { 'E', 'u', 'p' , 'h', 'o', 'r', 'i', 'a' }
Or even:
text = { 69, ... } -- I do not know the ASCII values by heart.
So there is not string *specific* tool, however there are *generic* tools
that help you manipulate sequences.
Look here:
text = "Euphoria"
text = text[1..3]
puts (1, text)
-- Sets "Eup" on the screen.
text = text & "horia rulezz"
puts (1, text)
-- Sets "Euphoria rulezz" on the screen.
text = append (text, '!')
puts (1, text[10..length(text)])
-- Sets "rulezz" on the screen.
print (1, length(text))
-- Sets 15 on the screen, which is the length of the sequence Text
puts (1, text[1])
-- Sets 'E' on the screen.
print (1, text[1])
-- Sets 69 on the screen.
As you see, the way the date should be interpretered does not have to be
specified, and it is up to you, to choose *how* you want to use, calculate
with, or display certain data.
A string is a sequence of ASCII values, and thus a one-dimensional array of
values, which in Euphoria eyes could just have been a highscore list, a list
of pallete-indexes or handles.
The interpretation of the data is up to *you*. Puts () will send the data to
a device. Your video coard interpreters ASCII values as characters to be
displayed. print () will however first convert your value to text.
Since "A" = {65}, however "65" equals {54, 53}
You can index sequences:
some_text = "yes"
print (1, some_text[1] ) -- Displays "121"
You can slice them:
print (1, some_text[1..2] ) -- Displays " { 121, 101 } "
puts (1, some_text[2..3] ) -- Displays "es"
But be warned:
print (1, some_text[1..1] )
-- Displays " { 121 } " instead of "121" since you sliced the sequence,
rather than index-ed it.
I hope this clears it all up a bit.
If you have more questions, feel free to post them.
I can also recommend, the tutorial by David Gay.
Its a program that teaches you Euphoria. The full source code is provided.
At the 'other sites related to Euphoria' section of Euphoria's page, you can
find this 'Beginners Guide to Euphoria'
Ralf Nieuwenhuijsen
nieuwen at xs4all.nl
ralf_n at email.com
UIN: 9389920