1. RTFM, was: Liberty Basic

At 12:50 AM 4/26/98 -0500, Kasey wrote:
>        if you can change how the file is writen (so that it starts with
>the number of entries in it)
>        you could do somthing like this (no real error checking in this, nor
>have I tried it, but it should be close)

See the code at end of this message -- it IS tested,
and doesn't need any changes or includes.

>------------begin code snipet---------
>include dim.e --- o.k. this is my include, but it's what I'd use
>                -- because for me it's the easiest way.
>include get.e --- net this to use get()
>
>function read_data()
>integer fn,size
>sequence instr, data
>
>fn = open ("data.dat","r")--- replace data.dat with  file your reading
>if fn = -1 then
>        printf(1,"can't open data.dat","")
>        abort(99)
>        end if
>size = get(fn)
>size = size[2]
>data = dim({size,2}) --- create a [size][2] array
>for t = 1 to size do
>        instr = get(fn)
>        data[t][1] = instr[2] --- read question #t
>        instr = get(fn)
>        data[t][2] = instr [2] --- and then the answer
>end for --- notice the lack of error checking in the above loop
>        --- the real prog would neet to watch for EOF
>
>return data
>end function
>---------end code sample------------
>
>you'd have to change your data file to from:
>
> How old are you?
> 37
> What time of day is it?
> 2pm
>
>
>to :
>
>{2}
>{How old are you?}
>{37}
>{What time of day is it?}
>{2pm}
>
>        I'm not positive you'd need the braces ( the { and } symbols)
>(do I need them? anyone ??) but that's how my data files are and they work...

Answer: no, you don't need the braces in a text file.

>        The reason for {2} at the top is so if the data file is
>changed as long as it starts with the total number of question/answer
>pairs (pairs not q + a!) the function can read it correctly.

Euphoria will handle all this automatically. You do not need
to dim anything, or count the lines in your data file. Computers
are good at counting.

>        the reason I used my dim.e and the dim function is so
>I didn't have to worry about getting my appends and &'s right, plus
>I get some independance between how I read the file and how the data
>is stored in the vaiable (plus dim.eis less than 20 lines against my
>300 total

You can do it in 18 or less with pure Euphoria (including the
EOF checking) It works fine, and needs no includes:
(It would take fewer lines if the questions and answers
 were packed into the same sequence, but that wasn't
 what was wanted)

----------------------- code begins -------------------------------
atom fn -- handle for file
sequence question, answer -- storage for q's and a's
         question = {}    -- must be empty when you start
         answer = {}      -- ditto

object input  -- must be declared as object, not sequence

fn = open("QUESTION.TXT","r")  -- open the question file as (fn)
if fn = -1 then abort(1) end if

 while 1 do         -- do forever

  -- get a question:
     input = gets(fn) -- read a line from your file
     if atom(input) then exit -- EOF encountered, so forever is over
     end if
     question = append(question,input) -- tack on to question list

  -- now get the answer:
     input = gets(fn) -- get next line
     if atom(input) then exit -- EOF
     end if
     answer = append(answer,input) -- tack on to answer list

 end while
close(fn)
Spam Haiku:-------------------
Silent, former pig
One communal awareness
Myriad pink bricks
-------------------------------------

new topic     » topic index » view message » categorize

2. Re: RTFM, was: Liberty Basic

>(It would take fewer lines if the questions and answers
> were packed into the same sequence, but that wasn't
> what was wanted)
Hi
OK I will bite.What do you mean packed into the same seq?
Is there a more simpleer way?.And thanks for all the help it works now and I
know why.I went back and checked my other programs and I was real close but
left out a few { } in the right place.Also am I useing with trace right like
these
with trace
trace(1)
because when I run the q file example(question[answer]) program it does not
give me what the varables are in the program and what they contain at the
given line of code like when it reads from a file I can not see it do the
get and append thing.
Thanks aigan for all the help.
In Christ
David Mosley
mosley01 at geocities.com
or
david08 at juno.com

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

3. Re: RTFM, was: Liberty Basic

At 03:25 AM 4/26/98 -0600, David Mosley wrote:

>Hi
>OK I will bite.What do you mean packed into the same seq?
>Is there a more simpleer way?.

Not really simpler, just different: instead of keeping two
lists (q's, and a's) you just make a single sequence in
this form:
QandA = {{question1,answer1},{question2,answer2}}
then you refer to them this way:
printf(1,"%s",{QandA[1][1]}) -- asks question #1
printf(1,"%s",{QandA[1][2]}) -- shows the answer for #1

Is that better or worse? For me, it is a little more work and
a bit less clear, but that's a personal preference. If you were
going to change the questions or answers, and then save them
back to disk, you'd probably want to use this form. The whole
thing could be written with a single "print(fn,QanA)" -- but,
it would not be easy to edit with a text editor anymore.
You win some, you lose some.

And thanks for all the help it works now and I
>know why.I went back and checked my other programs and I was real close but
>left out a few { } in the right place.Also am I useing with trace right
like these
>with trace
>trace(1)

Yes.
Trace is great, but also a little confusing, because of the
way it has to display stuff. Since a sequence or object
may contain anything, trace just has to show the contents
in a "generic" way, which often _looks_ way different from
what you put into the sequence. Sometimes it's better
to stick little printf() lines in to be sure you can get out
what you are putting in.

>because when I run the q file example(question[answer]) program it does not
>give me what the varables are in the program and what they contain at the
>given line of code like when it reads from a file I can not see it do the
>get and append thing.

You won't be able to see much of a sequence with trace,
anyway (it runs out of screen space). One thing has me
puzzled : Euphoria trace lets us check a variable that's
not shown on the screen by typing "? var"  (if var = 23,
then we'll see "var=23" pop up at the bottom of the screen.
Why can't we type "? seq[2]" to see the contents of the
second bit of seq?

Irv

Spam Haiku:-------------------
Silent, former pig
One communal awareness
Myriad pink bricks
-------------------------------------

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

Search



Quick Links

User menu

Not signed in.

Misc Menu