Page 1 of 2
Finding max in a text file...
Posted: Fri Nov 22, 2013 10:00 pm
by Sekoshiba
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
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!

Re: Finding max in a text file...
Posted: Fri Nov 22, 2013 10:16 pm
by dunbarx
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"?
Re: Finding max in a text file...
Posted: Fri Nov 22, 2013 10:43 pm
by Klaus
Are you sure that "specialFolderPath("./")" is valid Livecode specialfolderpath code?
BTW, what are you trying to achieve?
Re: Finding max in a text file...
Posted: Fri Nov 22, 2013 11:52 pm
by SparkOut
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?
Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 9:42 am
by Sekoshiba
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?
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?
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

Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 12:13 pm
by Klaus
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
Best
Klaus
Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 5:43 pm
by Sekoshiba
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
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...
Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 5:52 pm
by Klaus
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
Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 6:08 pm
by Sekoshiba
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
Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 6:25 pm
by Klaus
Hi Sekoshiba,
you cannot put a function inside of another handler!
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
Re: Finding max in a text file...
Posted: Sat Nov 23, 2013 7:15 pm
by FourthWorld
Klaus wrote:you cannot put a function inside of another handler!

True, but the attempt at a closure-like construct was intriguing just the same.

Re: Finding max in a text file...
Posted: Sun Nov 24, 2013 5:13 pm
by Sekoshiba
Klaus wrote:Hi Sekoshiba,
you cannot put a function inside of another handler!
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
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

Re: Finding max in a text file...
Posted: Sun Nov 24, 2013 5:44 pm
by Klaus
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
Re: Finding max in a text file...
Posted: Sun Nov 24, 2013 6:18 pm
by Sekoshiba
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!
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

Re: Finding max in a text file...
Posted: Sun Nov 24, 2013 6:48 pm
by Klaus
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
