1. Reading an Array

David Mosley:
>The problem I think is that in basic it is easy to get an array set up in
>euphoria it is not.What I want to do is this
>my data file looks like this
>How old are you?
>37
>What time of day is it?
>2pm
>In basic I would do this like
>open "l1.dat" for input as #1
>for t=1 to 2
>input #1,a$ 'read in data
>a1$(t)=a$ ' put in to array
>input #1,q$
>q1$(t)=q$
>next t
>close #1

Assuming you're using a text file with each item on a separate line, try
this:
----------
integer filenumber
sequence questions, answers

filenumber = open( "l1.dat", "r" )
if fn = -1 then
      puts(1, "Couldn't open l1.dat\n")
      abort(1)
end if

questions = {}
answers = {}
for t = 1 to 2 do
   questions = append( questions, gets(filenumber) )
   answers = append( answers, gets( filenumber ) )
end for
close( filenumber )
--------------
     The only real difference is that the sequences must be initialized and
that you're appending instead of assigning.  If you know how many lines of
data you're going to read, (as you do in that example)  you could initialize
the sequences to the right size first and then assign the data instead of
appending it.

questions = {"", ""}
answers = {"", ""}
for t = 1 to 2 do
   questions[t] = gets(filenumber)
   answers[t] = gets( filenumber )
end for

     That should work for your test case, but if you don't know how many
questions and answers there are or you want to make sure there's an even
amount (to prevent premature-EOF errors if the file's corrupt) then you
should check each line before you append it.

----------------------
integer filenumber
sequence questions, answers
object line         --data testing object

filenumber = open( "l1.dat", "r" )
if fn = -1 then
      puts(1, "Couldn't open l1.dat\n")
      abort(1)
end if

questions = {}
answers = {}

while 1 do
  line = gets( filenumber )
         if atom(line) then    --test it,
             exit                  -- -1 is returned at end of file
         end if
  questions = append( questions, line )

  line = gets( filenumber )
         if atom(line) then
             exit
         end if
  answers = append( answers, line )
end while

close( filenumber )
-----------------------------------
That'll read to the end of file.  Slightly more code, but it's safer.  Then
you'd use something like:

if length( questions ) != length( answers ) then
    --whatever you want to do if there aren't an even amount
end if

I tried Liberty Basic too...  Didn't have much luck with it, though...it was
too confusing.  smile

Hope that helps,
Falkon

new topic     » topic index » view message » categorize

2. Re: Reading an Array

>
>I tried Liberty Basic too...  Didn't have much luck with it, though...it
was
>too confusing.  smile
>
Hi
The problem with LB is that you have to work with dll's in order to make any
thing work and that is a headache by it self.I have found out that windows
is just a pian to work with you have to know about/least 100 or so API calls
in order to get anything done.

In Christ
David Mosley
mosley01 at geocities.com
or
david08 at juno.com

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

Search



Quick Links

User menu

Not signed in.

Misc Menu