Colin Holgate wrote:
> 
> >One method that nobody mentioned in any of the replies to your
> >question is extracting the words from the string rather than
> >deleting the spaces:
> 
> I thought of that too, but didn't post it because it would also get
> rid of returns. I'm pretty sure that he wants to get rid of returns
> as well, but he didn't ask for that, so I let it go.



Use the itemdelimiter and you can leave the returns:

This one is fastest by 40 to 50% in all cases I tried, I think that's
largely because it only counts the items in the text once:

on removespaces1 theText
  the itemdelimiter = " "
  newtext = ""
  wordTotal = theText.item.count
  repeat with x = 1 to wordTotal
    newText = newText & theText.item[x]
  end repeat
  return newText
end


For short strings this is faster than the recursion but slower on large
chunks of text:

on removespaces2 theText
  the itemdelimiter = " "
  newtext = ""
  repeat with x = 1 to theText.item.count
    newText = newText & theText.item[x]
  end repeat
  return newText
end


The first of these recursive methods is a hair faster (~4%) than the
second, but both do too much item counting.

on killSpaces inStr
  the itemdelimiter = " "
  if inStr.item.count < 2 then return inStr.word[1]
  return inStr.item[1] & killSpaces(inStr.item[2..the maxInteger])
end

on removespacesrecursively theText
  the itemdelimiter = " "
  if theText.item.count > 1 then
    theText = theText.item[1] & removespacesrecursively
(theText.item[2..the maxinteger]) 
  end if
  return theText
end



-- 
Carl West    [EMAIL PROTECTED]
617.262.8830 x246    

I have no superfluous leisure; my stay must be
stolen out of other affairs; but I will attend you awhile.

           - Isabella, Measure for Measure, Act 3 Scene 1

[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi  To post messages to the list,
email [EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo.  Thanks!]

Reply via email to