Correct syntax fpr appending data

Cubist at aol.com Cubist at aol.com
Mon Mar 6 15:51:17 EST 2006


In a message dated 3/6/06 10:59:12 AM, "Ben Bock" <benbock at msn.com> writes:
>
>What is the correct syntax for Rev to append data to the next line in a
>text file?
>I have a quiz in a standalone, and when the participant finishes, the file
>needs to save.  the next particpant's scores should go on the next line.
> I use a button at the end of the quiz:
>
>on mouseUp
>  set itemDelimiter to tab
>  put fld "quiz_item1" into item 1 of line 1 of field "data field" of stack 
"data sample"
>  put fld "quiz_item2" into item 2 of line 1 of field "data field" of stack 
"data sample"
>  put fld "data field " into url "file:test data.txt"
>  get url "file:test data.txt" 
>  put it into fld "sample field that proves to me something is going to the 
test data file"
>  save url "file:test data.txt"
>end mouseUp
>
>1) So how do I get the next person's data to enter the next line and save,
>without over writing the previous person's data?
   You need to *not* explicitly anoint a particular number as The One True 
Line-Number when you're putting your new data into that field. Something like 
this would work:

on mouseUp
  set itemDelimiter to tab
  put the number of lines in (field "data field" of stack "data sample") into 
Fred
  add 1 to Fred
  put fld "quiz_item1" into item 1 of line Fred of field "data field" of 
stack "data sample"
  TheRestOfTheCodeGoesHere

   The idea is that you find out which line you should put your data into, 
*before* you put your data into that line.

>2) Do you see flaws in this approach?
   Hmmm... not so much "flaws" as "differences in programming style". You're 
adding each piece of data to your field, as a separate operation; if I was 
writing this thing, I'd put all the new data into a variable, and then add that 
variable (which contains X number of pieces of data) to the field. Something 
like this:

on mouseUp
  set itemDelimiter to tab
  put the number of lines in (field "data field" of stack "data sample") into 
Fred
  add 1 to Fred

  put fld "quiz_item1" into item 1 of George
  put fld "quiz_item2" into item 2 of George
  put George into line Fred of field "data field" of stack "data sample"

  put fld "data field " into url "file:test data.txt"
  get url "file:test data.txt" 
  put it into fld "sample field that proves to me something is going to the 
test data file"
  save url "file:test data.txt"
end mouseUp

   The difference is that your way, you 'touch' the field many times; my way, 
I 'touch' the field only once. Why would anyone worry abut how many times 
they 'touch' a field? Because it takes Revolution less time to change the 
contents of a variable than to change the contents of a field. That is, variables are 
faster than fields. The actual speed difference is measured in milliseconds 
(if that much) for any one operation, but it *can* add up, depending on what 
you're doing, so I generally go for speed as and when I can. 
   All of which said, your way *will* work. And it may well be that a few 
milliseconds here and there simply will never be *noticed* by the users of your 
stack. So don't feel you *must* change!

   Hope this helps...



More information about the Use-livecode mailing list