Block of code not working

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

Block of code not working

Post by Nico_C_GHS » Mon Mar 31, 2014 9:09 am

This is the code :

Code: Select all

on preopencard
   setup_
   open file specialfolderpath("desktop") & "/mydata.txt" for read
   read from file specialfolderpath("desktop") & "/mydata.txt" until eof
   put it into tHighscores
   put tHighscores after fld output
   close file specialfolderpath("desktop") & "/mydata.txt"
   sort_
end preopencard

on setup_
   put empty into fld "output"
   put empty into fld "output2"
end setup_

on sort_
   put fld "output" into MyVariable
   set the itemdel to tab
   split MyVariable by lf and tab
   sort lines of MyVariable descending numeric by item 2 of each
   repeat with loop = 1 to the number of lines of "MyVariable"
      Put MyVariable[loop] into line loop of field output
   end repeat
end sort_
Here is my problem:
I can't get the lines of "MyVariable" to be sorted and re-displayed in the field "output".


N.B.
- The output (text) field has been named "output".
- tHighscore needs to be used to put data into the field one score at a time, I've attempted to have all data stored in it as an array from the .txt file on my desktop but for some reason, the scores aren't saving after one another and are instead only saving one at a time
Nico Colarusso | Gracemount High School

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

Re: Block of code not working

Post by Nico_C_GHS » Mon Mar 31, 2014 9:10 am

I have attached screenshots of the program as it is running.

The variable "hey" was replaced by "MyVariable" though for some reason hasn't changed. This is only a recent change and is unlikely to be what is effecting the program's output. I have had the same problem since before I changed the name of the container.
Attachments
Screen Shot 2014-03-31 at 09.05.07.png
Screen Shot 2014-03-31 at 09.04.25.png
Nico Colarusso | Gracemount High School

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

Re: Block of code not working

Post by Klaus » Mon Mar 31, 2014 10:25 am

Hi Nico,

1. I deleted the duplicate of this thread. 8)

2. I had this "déja vue" and found that almost all of your questions have already been answered here:
http://forums.runrev.com/viewtopic.php?f=22&t=18273
:shock:


Best

Klaus

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

Re: Block of code not working

Post by Nico_C_GHS » Tue Apr 01, 2014 2:12 pm

Hi Klaus,

I have already trawled through the post you linked and it didn't help because the questions I had asked already were looking for what to do. This time I'm looking for why the previous advice I was given hasn't worked.

I have the code above so there's nothing being kept back or hidden. I've checked and re-written the code twice today alone and still no change, so any useful advice would be appreciated.

-Nico
Nico Colarusso | Gracemount High School

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

Re: Block of code not working

Post by Klaus » Tue Apr 01, 2014 4:24 pm

I'm not sure we are talking about the same other thead!? 8)

Please read my comments carefully and try to memorize that important stuff!

Code: Select all

on preopencard
  setup_

  ## Why not use my ONE-liner for reading/writing data to file?
  put specialfolderpath("desktop") & "/mydata.txt" into tFile
  
  ## You need to check if the file is really present! 
  ## See the other thread
  put url("file:" & tFile) into tHighscores
  ## put tHighscores after fld "output"

  ## Maybe better? Or even INTO fld?
  put CR & tHighscores after fld "output"
  sort_
end preopencard

on setup_
     put empty into fld "output"
     put empty into fld "output2"
end setup_

on sort_
  put fld "output" into MyVariable
  set the itemdel to tab
  ##  split MyVariable by lf and tab
  ## As I wrote in that other namely thread: at this point MyVaiable is an ARRAY and does not have any lines you could sort!
  ##  sort lines of MyVariable descending numeric by item 2 of each
  ## !!
  
  ## So why not just use the SORT command with your simple TAB/CR delimited list?
  ## No need to mess with an array here!
  sort lines of MyVariable descending numeric by item 2 of each
  
  put MyVariable into fld "output"
  ## DONE!

  ## This is still an ARRAY, so that not works:
  ## repeat with loop = 1 to the number of lines of "MyVariable"
  ## AND when you use QUOTES ->"MyVariable" then it is NOT a variable but the STRING "MyVariable"

  ## Put MyVariable[loop] into line loop of field output
  ## end repeat
end sort_

Post Reply

Return to “Games”