Re: save and load sequences ??
And here is a very slightly improved version of Irv's solution.
Dan
<begin improved code>
--Tested code:
-- SAVE BUNCHES OF TEXT AND NUMBERS TO FILE AND READ IT BACK
include get.e --need it for the "get()" function to read whole file at once
atom fn
object topten
-- CREATE 10 INITIAL DUMMY NAMES AND ZERO'D SCORES & WRITE THEM TO FILE:
topten = repeat({"NaName",0},10)
fn = open("scores","w")
print(fn,topten)-- write them to file
close(fn)
-- READ THE SCORE FILE BACK & SHOW IT:
puts(1,"original scores\n")
topten = {}
fn = open("scores","r")
topten = get(fn) -- read entire score file into one sequence(object)
topten = topten[2]--assume success (success state returned in [1]), get file
-- each element in the sequence is one name/score pair
for i = 1 to length(topten) do
printf(1,"Name: %s Score: %d\n",topten[i]) -- show name/scores
end for
puts(1,"\n") -- spacer line
-- UPDATE A SCORE & SAVE IT:
topten[5][1] = "somebody"
topten[5][2] = 50
fn = open("scores","w")
print(fn,topten)
close(fn)
-- LOOK AT SCORES AGAIN & SHOW THEM AGAIN:
puts(1,"UPDATED SCORES\n")
topten = {}
fn = open("scores","r")
topten = get(fn)
topten = topten[2]
for i = 1 to length(topten) do
printf(1,"Name: %s Score: %d\n",topten[i])
end for
--
<end improved code>
----- Original Message -----
From: "irv" <irv at ELLIJAY.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Tuesday, September 12, 2000 8:23 PM
Subject: Re: save and load sequences ??
> On Tue, 12 Sep 2000, you wrote:
> > Hi Folks
> >
> > Ive got a game almost done but Im
> > trying to add running score that saves to disk file.
> >
> > I want to use the following sequence for my scores:
> >
> > sequence TopTen
> > TopTen = repeat({"NoName", 0} ,10)--This will be what it starts out with
> >
> > Ive been able to save it to disk
> > but reading it back has been a challenge for me.
> > Ive been saving it as a text file and reading it like such.
> > for x = 1 to 10
> > TopTen[x][1] --The text comes up fine
> > TopTen[x][2] --but the numbers dont come out right
> > end for
> >
> > I read and tried to understand The mydata.ex file
> > in the Demo directory but it is hard for me to follow.
> >
> > Any help here would be appreciated.
> >
> > Thank you
> > Gene
>
> --Tested code:
> include get.e
> atom fn
> object topten
> include get.e
> atom fn
> object topten
>
> topten = repeat({"NaName",0},10)
> fn = open("scores","w")
> print(fn,topten)
> close(fn)
>
> topten = {}
>
> fn = open("scores","r")
> topten = get(fn)
> topten = topten[2]
> for i = 1 to length(topten) do
> printf(1,"Name: %s Score: %d\n",topten[i])
> end for
> topten = repeat({"NaName",0},10)
> fn = open("scores","w")
> print(fn,topten)
> close(fn)
>
> topten = {}
>
> fn = open("scores","r")
> topten = get(fn)
> topten = topten[2]
> for i = 1 to length(topten) do
> printf(1,"Name: %s Score: %d\n",topten[i])
> end for
> --
> Regards,
> Irv
|
Not Categorized, Please Help
|
|