Can't get read/write and sorts to work
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Can't get read/write and sorts to work
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)
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 309 times
Nico Colarusso | Gracemount High School
Re: Can't get read/write and sorts to work
Hi Nico,
I am afraid you are making this more complicated than neccessary!
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
I would do this.
script of cd 1:
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
I am afraid you are making this more complicated than neccessary!
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
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 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
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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.
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
Re: Can't get read/write and sorts to work
Hi Nico,
I recommended earlier
Always remember how you would do this in "real life" with a pencil and a piece of paper!
Best
Klaus
yes, no wonder, the little stack is not very selfexplaining, and my SORT solution refers to the TEXT listNico_C_GHS wrote:... the line you are recommending doesn't fulfil the criteria of the project.
I recommended earlier
Do that, and you will see how complicated your life was before!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.
Always remember how you would do this in "real life" with a pencil and a piece of paper!
Best
Klaus
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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
Kind regards,
Nico
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
Kind regards,
Nico
Nico Colarusso | Gracemount High School
Re: Can't get read/write and sorts to work
Hi Nico,
That is just like writing to file, only the other way round
Best
Klaus
did you adjust the rest of your programme to "fit" my copied script?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.
"call up"? With the telephone? Or do you mean read the file back into your programme?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,...
That is just like writing to file, only the other way round
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
...
Klaus
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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?
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
Re: Can't get read/write and sorts to work
Hi Nico,
But it does not matter WHEN you sort your data, as long this happens BEFORE it is displayed!
Best
Klaus
Sure, what is the question?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?
But it does not matter WHEN you sort your data, as long this happens BEFORE it is displayed!
Best
Klaus
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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)
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
Re: Can't get read/write and sorts to work
Hi Nico,
Can't you just put the variable into a text field with some tab stops set?
Best
Klaus
well, I wrote exactly THAT a bit earlierNico_C_GHS wrote:1) To read the data from the file and put it into a variable
OK.Nico_C_GHS wrote:2) To sort the scores in that variable (I have code that does this step efficiently)
???Nico_C_GHS wrote:3) To display the scores and the corresponding username in an output field.
Can't you just put the variable into a text field with some tab stops set?
Best
Klaus
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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
Re: Can't get read/write and sorts to work
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
Best
Klaus
Still no idea what your "swapping" repeat loop does, but here a corrected script,
which should work in general, maybe not in detail
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
Klaus
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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?
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
Re: Can't get read/write and sorts to work
Hi Nico,
I think I found the problem!
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
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
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
...
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
...
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
-
- Posts: 44
- Joined: Wed Jun 12, 2013 2:36 pm
Re: Can't get read/write and sorts to work
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
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