Page 1 of 1
Writing data stream to text file
Posted: Mon Aug 31, 2015 6:13 pm
by tobx
Hey LiveCode! I'm trying to figure out how I can record the long time to a text file each time a user presses the space bar. Here is what I currently have:
Code: Select all
on keyDown spacebar
if the enableAddScore of this card = "true" then add 1 to gscore
put gscore into field "Score"
##testing save stuff
write the long time & tab to file tFile at end
set the enableAddScore of this card to "false"
end keyDown
tFile is accurate and already open for append from a previous stack but this code I currently have is not writing the long time + tab whenever the user presses the space bar. Any tips?
Re: Writing data stream to text file
Posted: Mon Aug 31, 2015 6:59 pm
by zaxos
Code: Select all
on keyDown spacebar
if the enableAddScore of this card = "true" then add 1 to gscore
put gscore into field "Score"
##testing save stuff
write the long time & tab to file tFile at end
put the result -- this could give you a clue
set the enableAddScore of this card to "false"
end keyDown
Re: Writing data stream to text file
Posted: Tue Sep 01, 2015 12:13 pm
by Klaus
Hi all,
"keydown" get send whenever the user type ANY character on his keyboard!
The parameter that come with "keydown" is the actual key the user pressed,
which may or may not be a SPACE!
So you first need to sort out any other keys like this, or this will happen when pressing ANY key:
Code: Select all
on keyDown tKey
if tKey <> SPACE then
pass keydown
end if
## NO need to QUOTE TRUE/FALSE, same for numbers!
if the enableAddScore of this card = TRUE then
add 1 to gscore
end if
put gscore into field "Score"
## I prefer the shorter URL syntax, no need to open or close the file etc.
put (the long time & TAB) AFTER url("file:" & tFile)
set the enableAddScore of this card to FALSE
end keydown
Best
Klaus
Re: Writing data stream to text file
Posted: Tue Sep 01, 2015 4:49 pm
by tobx
I see, the put + URL combo really cleans things up (and is easier to read for revision). Thanks for the suggestions, zaxos and Klaus! The data stream is working flawlessly!
Re: Writing data stream to text file
Posted: Tue Sep 01, 2015 5:21 pm
by FourthWorld
While the URL syntax is much easier to read, for append operations my understanding is that the convenience it offers the scripter comes at a significant cost to performance. IIRC it treats the file contents as a complete chunk, so "put after" will first read the entire file, append the data in memory, then write the whole thing out again. In contrast, when a file is first opened in append mode it will only write to the end of the file, so none of the data written previously is ever touched by subsequent writes.
I tested this hypothesis with the following code. Try a few runs with different values in the variable n defined on the first line. At 10 iterations "put URL" is still slower than "open for append" but not bad. However, as you increase the number of iterations to 1000 or 10000 the time for AppendData1 grows exponentially, whereas time for AppendData2 grows linearly:
Code: Select all
on mouseUp
put 1000 into n
put "This is a test" into s
put specialFolderPath("desktop")&"/TestDumpFile1.txt" into tFile1
put specialFolderPath("desktop")&"/TestDumpFile2.txt" into tFile2
--
-- "put URL"
put the millisecs into t
repeat n
AppendData1 s, tFile1
end repeat
put the millisecs - t into t1
--
-- "open for append"
put the millisecs into t
open file tFile2 for append
repeat n
AppendData2 s, tFile2
end repeat
close file tFile2
put the millisecs - t into t2
--
put url ("file:"& tFile1) into tData1
put url ("file:"& tFile2) into tData2
put "URL: "& t1 &" ms"&cr& "Append: "& t2 &" ms"&cr \
&"Files the same? "& (tData1 = tData2)
end mouseUp
on AppendData1 s, tFile
put s&cr after url ("file:"& tFile)
end AppendData1
on AppendData2 s, tFile
write s &cr to file tFile
end AppendData2
For tasks done infrequently it probably doesn't matter much which form you use, but for extremely frequent tasks like file I/O on every keystroke the speed will eventually make a noticeable difference as the size of the data in the file grows.
Re: Writing data stream to text file
Posted: Wed Sep 09, 2015 5:47 pm
by tobx
FourthWorld wrote:but for extremely frequent tasks like file I/O on every keystroke the speed will eventually make a noticeable difference as the size of the data in the file grows.
This is exactly what I'm doing and noticing so back to "write ... at end" it is. Awesome breakdown and demo, thanks so much!
While we're here, can anyone spot what's wrong with this line?
Code: Select all
put statround(ypercent / 100 * ticketvalue) into yticketswon
I keep getting the following error: "execution error at [this line] (Operators /: error in left operand), char 1"
I tried removing the extra spaces and checked the variables being used and still no luck. This output is vital to the data stream!
Re: Writing data stream to text file
Posted: Wed Sep 09, 2015 5:53 pm
by Klaus
HI tobx,
hm, must be something else, syntax is correct, just made little test:
Code: Select all
on mouseUp
put 100 into ypercent
put 100 into ticketvalue
put statround(ypercent / 100 * ticketvalue) into yticketswon
answer yticketswon
end mouseUp
and got 100 as exspected, no errors!
Best
Klaus
Re: Writing data stream to text file
Posted: Wed Sep 09, 2015 6:21 pm
by FourthWorld
tobx wrote:FourthWorld wrote:but for extremely frequent tasks like file I/O on every keystroke the speed will eventually make a noticeable difference as the size of the data in the file grows.
This is exactly what I'm doing and noticing so back to "write ... at end" it is. Awesome breakdown and demo, thanks so much
Glad that was helpful.
FWIW, when opening a file in append mode it will only write to the end of the file, so in those cases "...at end" isn't needed with the write command. Doesn't hurt if it's there, it just doesn't doesn't do anything that isn't already handled by the file opening mode.
Re: Writing data stream to text file
Posted: Fri Sep 11, 2015 4:13 pm
by tobx
FourthWorld wrote:"...at end" isn't needed with the write command.
Guess I was being too careful with that addition. Can't afford to lose any data for this study, noted! Thanks for all your help everyone! This community is AWESOME!
I'm still stuck on this line. I copy-pasted from Klaus to be sure I'm not doing anything weird:
Code: Select all
put statround(ypercent / 100 * ticketvalue) into yticketswon
Execution error in left operand, char 1. What does this even mean? Char 1 is the very beginning of the line, right? The first character of this like is the "p" in "put!"
Re: Writing data stream to text file
Posted: Sat Sep 12, 2015 5:13 pm
by jacque
Does ypercent have a numerical value?
Re: Writing data stream to text file
Posted: Sun Sep 13, 2015 7:00 pm
by tobx
jacque wrote:Does ypercent have a numerical value?
Wow, I'm really dumb. I had a line putting numbers & "%" into ypercent. I removed it and now everything works. Thanks so much for the suggestion!
Re: Writing data stream to text file
Posted: Mon Sep 14, 2015 5:07 pm
by jacque
Lol. Been there, done that.
