Finding max in a text file...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Sekoshiba
Posts: 31
Joined: Thu Sep 26, 2013 8:23 pm

Finding max in a text file...

Post by Sekoshiba » Fri Nov 22, 2013 10:00 pm

Hi guys,

I'm having some issues with filehandling...Namely having the .txt files generating in the right place and also creating a find maximum algorithm that both works and doesn't crash my program :shock:

At the moment my code is looking like this:

Code: Select all

on mouseUp
 open file specialFolderPath("./") & "/Score" for write  //On mac this syntax worked fine for generating the .txt just outside the applet file, but on windows it goes haywire...
    put field "Field" into tData
    write myText to file specialFolderPath("./") & "/Score"  //...Now it does not generate near the applet at all, usually outside the folder  :( 

repeat with x= 1 to 10   //...And then there's this, which no matter how many times I rewrite it and make sure no eternal loops are happening, just crashes the program...
   put x into tXtemp1
   put line x of tData into tXvtemp1
   if x > 1 then
      add -1 to x
      put x into tXtemp2
   put line x of tData into tXvtemp2
   end if
   
   if tXvtemp2 > line tXvtemp1 of tData then
      put tXvtemp2 into line tXtemp1 of tData
      put tXvtemp1 into line tXtemp2 of tData
      end if
   
end repeat
end mouseUp
...Any help would be deeply appreciated, I'm nearing the end of the implementation stage for a project, so the string noobish questions such as these from me will cease! I promise! :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Finding max in a text file...

Post by dunbarx » Fri Nov 22, 2013 10:16 pm

Hi.

A couple of things:

1- Always step through your code when you have issues. Set a breakpoint or two and watch.
2- It is never good practice to play with the index variable in a "repeat with.." construction. Leave it alone. Use other indexes as you will or use a different "repeat" form where you can manage the index manually.
3- Always step through... Oh yeah, said that already.

You have set up an infinite recursion. LC is not crashing, it is just exhausted. Put a breakpoint at the top of the repeat loop and watch what happens to the variable "x".

Write back with what you see.

Craig Newman

EDIT: Who is "max"?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding max in a text file...

Post by Klaus » Fri Nov 22, 2013 10:43 pm

Are you sure that "specialFolderPath("./")" is valid Livecode specialfolderpath code? 8)

BTW, what are you trying to achieve?

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Finding max in a text file...

Post by SparkOut » Fri Nov 22, 2013 11:52 pm

As Craig says, you need to look at different repeat forms to handle line by line comparisons. Tip: the "repeat for each <chunk> in <source>" form is very fast.
But I can't help wonder what you're trying to do? Surely you can just "sort tData ascending" and then take the value from the first line?

Sekoshiba
Posts: 31
Joined: Thu Sep 26, 2013 8:23 pm

Re: Finding max in a text file...

Post by Sekoshiba » Sat Nov 23, 2013 9:42 am

dunbarx wrote:Hi.

A couple of things:

1- Always step through your code when you have issues. Set a breakpoint or two and watch.
2- It is never good practice to play with the index variable in a "repeat with.." construction. Leave it alone. Use other indexes as you will or use a different "repeat" form where you can manage the index manually.
3- Always step through... Oh yeah, said that already.

You have set up an infinite recursion. LC is not crashing, it is just exhausted.
ah, yeah...i shall make us of breakpoints next time i have an error!

Indeed, i shall leave the ""repeat with" syntax alone in future. Erm... Sorry, what i meant to say in my original post was that it was generating an infinite loop and that is what crashes it.

Thanks eitherway :)
Klaus wrote:Are you sure that "specialFolderPath("./")" is valid Livecode specialfolderpath code? 8)

BTW, what are you trying to achieve?
I was trying to have the text file sit in the same folder as the application. As I said, on mac the 'specialfolderpath("./")' syntax does it perfectly. I assumed "./" would generate in the folder because it's used for setting the directory to graphics and such? :shock:

Thanks regardless :)
SparkOut wrote:As Craig says, you need to look at different repeat forms to handle line by line comparisons. Tip: the "repeat for each <chunk> in <source>" form is very fast.
But I can't help wonder what you're trying to do? Surely you can just "sort tData ascending" and then take the value from the first line?
Ooh, That does sound much easier! Thanks very much :)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding max in a text file...

Post by Klaus » Sat Nov 23, 2013 12:13 pm

Hi Sekoshiba,
I was trying to have the text file sit in the same folder as the application.
As I said, on mac the 'specialfolderpath("./")' syntax does it perfectly.
I assumed "./" would generate in the folder because it's used for setting the directory to graphics and such?
since this is NOT a valid "specialfolderpath" code, you are lucky that it works on the Mac,
it SHOULD have given you an error! So don't rely on this!

You can always get the path to your "standalone" folder with a little function like this:

Code: Select all

function FolderOfStandalone
  put the effective filename of this stack into tFolder
  set itemdel to "/"
  delete item -1 of tFolder
  return tFolder & "/"
end FolderOfStandalone
Then:
...
put field "Field" into url("file:" & FolderOfStandalone() & "/Score")
...
Please note my use of the shorter URL syntax for file access, mostly a one-liner :D


Best

Klaus

Sekoshiba
Posts: 31
Joined: Thu Sep 26, 2013 8:23 pm

Re: Finding max in a text file...

Post by Sekoshiba » Sat Nov 23, 2013 5:43 pm

Klaus wrote:
since this is NOT a valid "specialfolderpath" code, you are lucky that it works on the Mac,
it SHOULD have given you an error! So don't rely on this!

You can always get the path to your "standalone" folder with a little function like this:

Code: Select all

function FolderOfStandalone
  put the effective filename of this stack into tFolder
  set itemdel to "/"
  delete item -1 of tFolder
  return tFolder & "/"
end FolderOfStandalone
Then:
...
put field "Field" into url("file:" & FolderOfStandalone() & "/Score")
...
Please note my use of the shorter URL syntax for file access, mostly a one-liner :D
The function you provided gives me this error: "button "Button": compilation error at line 3 (Handler: error in command) near "function", char 1", I'm probably just making a mistake or calling the function incorrectly I think...

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding max in a text file...

Post by Klaus » Sat Nov 23, 2013 5:52 pm

Hi Sekoshiba,

hmmm, copied my script and pasted into LC and could compile and call it without problems?
What did you script?

Oh, I had a typo in one of the scripts!
Here the correct one:

Code: Select all

...
put field "Field" into url("file:" & FolderOfStandalone() & "Score")
...
Best

Klaus

Sekoshiba
Posts: 31
Joined: Thu Sep 26, 2013 8:23 pm

Re: Finding max in a text file...

Post by Sekoshiba » Sat Nov 23, 2013 6:08 pm

Here's what I coded, Just tried to activate the function w/ a button press

Code: Select all

on mouseUp
 
 function FolderOfStandalone
  put the effective filename of this stack into tFolder
  set itemdel to "/"
  delete item -1 of tFolder
  return tFolder & "/"
end FolderOfStandalone

put field "Field" into url("file:" & FolderOfStandalone() & "Score")
 
end mouseUp

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding max in a text file...

Post by Klaus » Sat Nov 23, 2013 6:25 pm

Hi Sekoshiba,

you cannot put a function inside of another handler! 8)

Do this:

Code: Select all

on mouseUp
  put field "Field" into url("file:" & FolderOfStandalone() & "Score")
end mouseUp

function FolderOfStandalone
  put the effective filename of this stack into tFolder
  set itemdel to "/"
  delete item -1 of tFolder
  return tFolder & "/"
end FolderOfStandalone
Or put the function into the stack script, so you can access it from everywhere you need!


Best

Klaus

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Finding max in a text file...

Post by FourthWorld » Sat Nov 23, 2013 7:15 pm

Klaus wrote:you cannot put a function inside of another handler! 8)
True, but the attempt at a closure-like construct was intriguing just the same. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Sekoshiba
Posts: 31
Joined: Thu Sep 26, 2013 8:23 pm

Re: Finding max in a text file...

Post by Sekoshiba » Sun Nov 24, 2013 5:13 pm

Klaus wrote:Hi Sekoshiba,

you cannot put a function inside of another handler! 8)

Do this:

Code: Select all

on mouseUp
  put field "Field" into url("file:" & FolderOfStandalone() & "Score")
end mouseUp

function FolderOfStandalone
  put the effective filename of this stack into tFolder
  set itemdel to "/"
  delete item -1 of tFolder
  return tFolder & "/"
end FolderOfStandalone
Or put the function into the stack script, so you can access it from everywhere you need!


Best

Klaus
ah! Thank you very much! I shall try this :D

Edit: Works like a charm, still unsure how to go about the sorting though...

atm I have the sort button like...

Code: Select all

On mouseup
 put url("file:" & FolderOfStandalone() & "Score") into tData

end mouseup
...But i'm still fairly uncertain on how to code the rest, I checked in the directory for the "Sort ascending" syntax, but I'm still pretty clueless as to how to use it :cry:

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding max in a text file...

Post by Klaus » Sun Nov 24, 2013 5:44 pm

Hi Sekoshiba,

are looking for something like this:

Code: Select all

On mouseup
   put url("file:" & FolderOfStandalone() & "Score") into tData

   ## tData now contains the content of the text fiel, now sort as you like:
   sort LINES of tData ASCENDING
   put tData into fld "tdata field"
end mouseup
?

Check the dictionary "SORT" for more options!


Best

Klaus

Sekoshiba
Posts: 31
Joined: Thu Sep 26, 2013 8:23 pm

Re: Finding max in a text file...

Post by Sekoshiba » Sun Nov 24, 2013 6:18 pm

Klaus wrote:Hi Sekoshiba,

are looking for something like this:

Code: Select all

On mouseup
   put url("file:" & FolderOfStandalone() & "Score") into tData

   ## tData now contains the content of the text fiel, now sort as you like:
   sort LINES of tData ASCENDING
   put tData into fld "tdata field"
end mouseup
?

Check the dictionary "SORT" for more options!


Best

Klaus
Woohoo! For what I'm using tData for I changed the sort to "descending" and it works perfectly! Thanks Klaus! :D

Would there be a way to include names within the sort, like say the structure of tData was

"John 46
Ewan 43
Bill 97"

...then would there be a way for the lines to sort regardless of the non-numerical data being there? If I could accomplish this without needing to add arrays also, that would be great :D

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding max in a text file...

Post by Klaus » Sun Nov 24, 2013 6:48 pm

You can:
...
sort lines of fld 1 numeric by word 2 of each
...
where field 1 is your name list:
John 46
Ewan 43
Bill 97

If that is what you mean :D

Post Reply