Can't get read/write and sorts to work

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Can't get read/write and sorts to work

Post by Nico_C_GHS » Thu Dec 05, 2013 3:18 pm

I am constructing a Highscores table and so far I have the following problem:

I am unable to bring in data from a saved .txt file which contains the Username and Score of the players in the game (all highscores are kept in the file so that there can be a continuously full table)

The reason for bringing the data back out of the .txt file is so that I can sort and display it.
Since I'm required to write out a full algorithm for the sort, the code I have used is slightly longer than it should be. Nevertheless, it works perfectly. I just need to figure out a way to bring in the data from the .txt file to sort ONLY the numbers (scores) and then display both the sorted list of scores and the corresponding username for each score.

Any advice would be fantastic, I'm extremely stumped by this and it's the last thing I need to finish coding before my program is finished.

I have included a duplicate of the 2 cards from the stack that influence one another for the highscores table (one of which being the highscores table card)
Attachments
Demonstration.livecode.zip
LC 6.0.1
(90.04 KiB) Downloaded 305 times
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Thu Dec 05, 2013 4:05 pm

Hi Nico,

I am afraid you are making this more complicated than neccessary! 8)

Since you are only managing USERNAME and his HIGHSCORE there is no need for an array!
A simply TAB & CR delimited list/text file will do! AND this is easy peasy to sort :-D

I would do this.
script of cd 1:

Code: Select all

Global gUsername
Global gUsername
Global gScore
Global datafilename
Global tempblock
Global tempx

on mouseUp
   put the text of fld "username" into gUsername
   put the text of fld "userScore" into gScore
   
   ## I guess TEMPBLOCKwill hold all other usernames/highscore pairs, right?
   if tempblock = EMPTY then
      put gUserName & TAB & gScore into tempblock
   else
      ## APPEND:
      put CR &  gUserName & TAB & gScore AFTER tempblock
   end if
   
   put specialfolderpath("desktop") & "/mydata.txt" into datafilename
   if there is a file DataFileName then
      
      ## APPEND:
      put CR & tempblock after url("file:"& datafilename)
      ## No need for BINFILE, this is just text!
   else
      put tempblock into url("file:"& datafilename)
   end if
   wait 1.5 seconds
   go card "highscores"
end mouseUp
But sorry, I have no idea what the script of cd 2 should/will do???

but some observations her:
1. You did not define TEMPBLOCK as a global!
2. "answer line 1 of specialfolderpath("desktop") & "/mydata.txt"
Does not answer the first line of the CONTENT of htat file, but just the full filename!
So the rest will not work as exspected!

3. No idea what you are "swapping" around in the repat loop?
If you use my solution with the text file (name TAB score) you can simply
sort that so the highest scores are on top with:
...
set itemdel to TAB
sort lines of tList numeric DESCENDING by item 2 of each
...
Et voila, highest score on top of the list!

Maybe that helps.


Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Thu Dec 05, 2013 4:14 pm

Hi Klaus,

The swapping part of the repeat is the full construct of the sorting algorithm that I've used, if you check the original post I said that I used that because I wasn't able to use the shorter version, the line you are recommending doesn't fulfil the criteria of the project.

As for the code and recommendations for the first card, I will need to implement that either tomorrow or slightly later and I will get back with the results.
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Thu Dec 05, 2013 4:20 pm

Hi Nico,
Nico_C_GHS wrote:... the line you are recommending doesn't fulfil the criteria of the project.
yes, no wonder, the little stack is not very selfexplaining, and my SORT solution refers to the TEXT list
I recommended earlier 8)
Nico_C_GHS wrote:As for the code and recommendations for the first card, I will need to implement that either tomorrow or slightly later and I will get back with the results.
Do that, and you will see how complicated your life was before! :D
Always remember how you would do this in "real life" with a pencil and a piece of paper!


Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Mon Dec 09, 2013 9:50 am

Hi Klaus,

Firstly, I tried copying and pasting the code that you sent and after reading through it a little, I went to run it. The program still isn't working.

As for the stack that I uploaded not being very self explanatory, I can re-upload it with internal commentary if you'd like?

Also, I understand fully that to sort the list I could use a very simple and easy procedure with very few lines of code that is undoubtedly easier to come to terms with in terms of it's complexity but I can't use that easy version of the code. The criteria for the project that I'm doing requires me to write out the full algorithm. Besides, the sort works anyway. The only thing that doesn't work about it is that I can't figure out how to call up the list I saved so that I can sort it and furthermore, I can't get it to display anything in the output field whatsoever.
The sort actually works though :P

Kind regards,
Nico
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Mon Dec 09, 2013 1:23 pm

Hi Nico,
Nico_C_GHS wrote:Hi Klaus,

Firstly, I tried copying and pasting the code that you sent and after reading through it a little, I went to run it. The program still isn't working.
did you adjust the rest of your programme to "fit" my copied script? 8)
Nico_C_GHS wrote:The only thing that doesn't work about it is that I can't figure out how to call up the list I saved so that I can sort it and furthermore,...
"call up"? With the telephone? Or do you mean read the file back into your programme?
That is just like writing to file, only the other way round :D

Code: Select all

...
put specialfolderpath("desktop") & "/mydata.txt" into tDataFile
if there is a file tDataFile then
  put url("file:" & tDataFile) into theVariable
else
 answer "No file so far!"
end if
...
Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Tue Dec 10, 2013 3:01 pm

Hi Klaus,

I played about with some code by myself and I eventually got the part working that displays the data.

Could you help me with sorting the data from the datafile?
or
Sorting the data when it goes INTO the datafile?
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Tue Dec 10, 2013 3:53 pm

Hi Nico,
Nico_C_GHS wrote:Could you help me with sorting the data from the datafile?
or
Sorting the data when it goes INTO the datafile?
Sure, what is the question?
But it does not matter WHEN you sort your data, as long this happens BEFORE it is displayed! 8)


Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Tue Dec 10, 2013 4:15 pm

Hey,

I'm just trying to figure out the code that I would need to do the following:

1) To read the data from the file and put it into a variable
2) To sort the scores in that variable (I have code that does this step efficiently)
3) To display the scores and the corresponding username in an output field.

Those^ are the things that I am stuck with (minus the 2nd step)
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Tue Dec 10, 2013 4:26 pm

Hi Nico,
Nico_C_GHS wrote:1) To read the data from the file and put it into a variable
well, I wrote exactly THAT a bit earlier 8)
Nico_C_GHS wrote:2) To sort the scores in that variable (I have code that does this step efficiently)
OK.
Nico_C_GHS wrote:3) To display the scores and the corresponding username in an output field.
??? :shock:
Can't you just put the variable into a text field with some tab stops set?


Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Mon Dec 16, 2013 10:19 am

Hi Klaus, this is my code so far for the Highscores card:

Code: Select all

Global gScore
Global tempx
Global datafilename
Global gUsername
Local Swapper
Local tItem
Local tHighscores

on preopencard 
   repeat with loop = 1 to the number of lines of file specialfolderpath("desktop") & "/mydata.txt"
   open file url(specialfolderpath("desktop") & "/mydata.txt") for read
   
   read from file url(specialfolderpath("desktop") & "/mydata.txt") until EOF
   put line loop of specialfolderpath("desktop") & "/mydata.txt" into tHighscores[loop]
   get the keys of tHighscores
   repeat with inner = 1 to the number of  lines of tHighscores
      repeat with outer = 1 to the number of  lines of tHighscores
         if gScore[outer] < gScore[inner] then
            
            put gScore[inner] into Swapper
            
            put gScore[outer] into gScore[inner]
            
            put Swapper into gScore[outer]
            
         end if
         
         next repeat
      end repeat
   end repeat 
   end repeat
   
   put empty into fld "output"
   put tab & "Name" & tab & "Score" into line 1 of fld "output"
   
   
   repeat with loopx = 1 to the number of  lines of tHighscores
      
      put line loopx of tHighscores into line loopx of tempx
      put tempx[loopx] & tab into line loopx of fld "output"
      
      close file specialfolderpath("desktop") & "/mydata.txt"
   end repeat 
   
end preopencard
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Mon Dec 16, 2013 12:50 pm

And, does it work? I highly doubt!

Still no idea what your "swapping" repeat loop does, but here a corrected script,
which should work in general, maybe not in detail 8)

Code: Select all

Global gScore
Global tempx
Global datafilename
Global gUsername
Local Swapper
Local tItem
Local tHighscores

on preopencard 
   
   ## Create pathname ONCE!
   put specialfolderpath("desktop") & "/mydata.txt" into tFile
   
   ## put content of file into variable ONCE!
   put url("file:" & tFile) into tFileContent
   
   ## Now loop through content:
      repeat with loop = 1 to the number of lines of tFileContent   
         put line loop of tFileContent into tHighscores[loop]
         repeat with inner = 1 to the number of  lines of tHighscores
               repeat with outer = 1 to the number of  lines of tHighscores
                     if gScore[outer] < gScore[inner] then                           
                           put gScore[inner] into Swapper                           
                           put gScore[outer] into gScore[inner]                           
                           put Swapper into gScore[outer]                           
                     end if    

                     ## At this point, the script AUTOMATICALLY goes to the NEXT repeat! 8-)                 
                     ## next repeat
               end repeat
         end repeat 
      end repeat
      
      put empty into fld "output"
      put tab & "Name" & tab & "Score" into line 1 of fld "output"
      
      repeat with loopx = 1 to the number of  lines of tHighscores      
            put line loopx of tHighscores into line loopx of tempx
            put tempx[loopx] & tab into line loopx of fld "output"
            
          ## Why close a file X times (the number of lines of tHighScores)???
          ## See my URL syntax above, no need to close a file with that snytax!
         ##  close file specialfolderpath("desktop") & "/mydata.txt"
      end repeat    
end preopencard
Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Wed Dec 18, 2013 2:49 pm

Hi Klaus,

After using your code, I get no output whatsoever. I really don't understand what the problem is here. The code you used looks pretty solid and it seems like it should work but nothing is being put in the output fields. I'm sorry for all the time you've taken on this for it to not work, it must be something I'm doing. I've considered your ideas of using a more simple sorting algorithm and decided that it is probably the best idea at the moment. Could I be a nuisance and ask for your help with the more simple sorting method you recommended before?
Nico Colarusso | Gracemount High School

Klaus
Posts: 13865
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can't get read/write and sorts to work

Post by Klaus » Wed Dec 18, 2013 5:11 pm

Hi Nico,

I think I found the problem!

Code: Select all

...
## Now loop through content:
      repeat with loop = 1 to the number of lines of tFileContent   
         put line loop of tFileContent into tHighscores[loop]
         repeat with inner = 1 to the number of  lines of tHighscores
               repeat with outer = 1 to the number of  lines of tHighscores
...
Since tHighScores is an ARRAY! it does not have any "number of lines"!
You need to count its KEYS and use that in your repeat loop

Code: Select all

...
put the num of keys of tHighscores into tNumberOfKeys
      repeat with loop = 1 to tNumberOfKeys   
         put line loop of tFileContent into tHighscores[loop]
         repeat with inner = 1 to tNumberOfKeys
               repeat with outer = 1 to tNumberOfKeys
...
WHAT exactly is in your "highscore" file?
And how is the content organized?
COMMA or TAB delimited list?

Whatever:
to sort a list of TAB delimited items:
name TAB 123
name2 TAB 2
etc...

Sort by name:
...
sort lines of tList
...
Sort numeric by item 2
...
set itemdel to TAB
sort lines of tList numeric by item 2 of each
...

Best

Klaus

Nico_C_GHS
Posts: 44
Joined: Wed Jun 12, 2013 2:36 pm

Re: Can't get read/write and sorts to work

Post by Nico_C_GHS » Fri Jan 03, 2014 4:57 pm

Hi Klaus,

The Highscore list contains the username and score of every player to have achieved any score at all.

The items in the list are stored as " name TAB score CR" so that the list is always kept semi-neat.

As for the code you think you've fixed, I have been trying to make sense of your final statement. It would appear that you are trying to sort a list of scores by name for me, which is totally the opposite of what I was aiming for. I don't understand what it was you were trying to suggest with that statement at all, could you elaborate?

Thanks
Nico
Nico Colarusso | Gracemount High School

Post Reply

Return to “Games”