Adding Numeric Increments

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Adding Numeric Increments

Post by bidgeeman » Thu Jun 25, 2009 12:37 am

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

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Thu Jun 25, 2009 1:59 am

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

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 25, 2009 2:08 am

Hello Mark and thank you :)

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

Cheers
Bidge

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Thu Jun 25, 2009 2:44 pm

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 
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 25, 2009 10:51 pm

Hi BvG.
I tried that code but it only puts a"0" in front of every line for some reason.

Cheers
Bidge

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Thu Jun 25, 2009 11:03 pm

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

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 25, 2009 11:19 pm

Hehehe thanks Bernd.
I noticed another Typo with the case "Text" as opposed to "text".
Does this make a difference?

Cheers
Bidge

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Fri Jun 26, 2009 12:07 am

bidge,
I noticed another Typo with the case "Text" as opposed to "text".
Does this make a difference?
No

regards
bernd=Bernd

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Fri Jun 26, 2009 12:13 am

Well....they both work perfectly! Thank you all again for your help.

Cheers
Bidge

Post Reply