Page 1 of 1

Adding Numeric Increments

Posted: Thu Jun 25, 2009 12:37 am
by bidgeeman
Hello.
I am trying to learn the repeat phrase. So far I am familiar with:

Code: Select all

on mouseUp
put Field "Text" into tFiles
repeat with i = 1 to the number of lines of tFiles
      put "000" before line i of tFiles
      put tFiles into field "Text"
      end repeat
end mouseUp
I have a list of text files and want to add a number to the front of them that increases numerically ie, 001 Text, 002 Text, 003 Text, etc but I'm not sure how to write this into the above code. Can someone please show me how this is implemented?

Many thanks for help.
Cheers
Bidge

Posted: Thu Jun 25, 2009 1:59 am
by Mark Smith
Try this:

Code: Select all

on mouseUp 
  put Field "Text" into tFiles
  put 0 into tCount
  repeat for each line tFile in tFiles
      add 1 to tCount
      put format("%03d", tCount) & tFile & cr after tChangedFiles -- see the 'format' function in the docs
  end repeat
  delete char -1 of tChangedFiles -- delete trailing cr
  put tChangedFiles into field "Text" 
end mouseUp 
"repeat for each" loops tend to run much faster than the "with n =..." loops, though it won't necessarily be noticeable unless your list of files is very long.

Best,

Mark Smith

Posted: Thu Jun 25, 2009 2:08 am
by bidgeeman
Hello Mark and thank you :)

I will study the format function in the docs to work out what's happening

Cheers
Bidge

Posted: Thu Jun 25, 2009 2:44 pm
by BvG
Format is most of the time superfluous, and makes the code harder to read. I suggest:

Code: Select all

on mouseUp
  put Field "Text" into tFiles
  set the numberformat to "000"
  put 0 into tCounter
  repeat for each line aLine in tFiles
    add one to myCounter
    put tCounter && aLine & return after tNewFiles
  end repeat
  delete char -1 of tNewFiles -- remove superfluous return
  put tNewFiles into field "text"
end mouseUp 

Posted: Thu Jun 25, 2009 10:51 pm
by bidgeeman
Hi BvG.
I tried that code but it only puts a"0" in front of every line for some reason.

Cheers
Bidge

Posted: Thu Jun 25, 2009 11:03 pm
by bn
bidge,

there is a typo in the original script: myCounter instead of tCounter
try:

Code: Select all

on mouseUp 
   put Field "Text" into tFiles 
   set the numberformat to "000" 
   put 0 into tCounter 
   repeat for each line aLine in tFiles 
      add one to tCounter 
      put tCounter && aLine & return after tNewFiles 
   end repeat 
   delete char -1 of tNewFiles -- remove superfluous return 
   put tNewFiles into field "text" 
end mouseUp
regards
Bernd

Posted: Thu Jun 25, 2009 11:19 pm
by bidgeeman
Hehehe thanks Bernd.
I noticed another Typo with the case "Text" as opposed to "text".
Does this make a difference?

Cheers
Bidge

Posted: Fri Jun 26, 2009 12:07 am
by bn
bidge,
I noticed another Typo with the case "Text" as opposed to "text".
Does this make a difference?
No

regards
bernd=Bernd

Posted: Fri Jun 26, 2009 12:13 am
by bidgeeman
Well....they both work perfectly! Thank you all again for your help.

Cheers
Bidge