Re: Reduce multiple characters to single occurrence (via do shell script?)
Re: Reduce multiple characters to single occurrence (via do shell script?)
- Subject: Re: Reduce multiple characters to single occurrence (via do shell script?)
- From: kai <email@hidden>
- Date: Fri, 25 Mar 2005 10:56:10 +0000
On Fri, 25 Mar 2005 01:15:43 -0600, Joseph Weaks wrote:
I want to clean up a string by reducing all double, triple, etc.
spaces to a single space.
I know how to do it with text item delimiters with something like:
set theString to "This has extra spaces.
I want them removed."
repeat
if theString contains " " then
set Applescript's text item delimiters to " "
set theString to text items of theString
set Applescript's text item delimiters to " "
set theString to theString as string
else
exit repeat
end if
end repeat
But, would a do shell script method be faster? Isn't there a one-liner
perl command that would reduce multiple occurrences of a character to
a single occurrence?
There are a number of reasons why one would opt for a shell script, Joe.
Sometimes a shell script might be the only way to achieve a particular
result. In addition, by the very nature of its english-like syntax,
AppleScript is usually rather more verbose than other languages - so a
shell script may offer the advantage of brevity. However, any external
call - whether it be to an application, a scripting addition or a shell
script - will usually carry some kind of overhead. In certain cases,
that overhead might be outweighed by efficiencies offered by the
target. In others, there are no speed benefits to be gained at all.
Here, and with this particular example, your script does the job
roughly 40 to 60 times faster than an equivalent shell script. So your
choice really depends on whether you are looking for speed or brevity.
You could abbreviate your routine (but only very slightly) by combining
the repeat loop with the if/then statement, as in the following
handler's repeat loop:
-----------------
to stripMultiples of c from s
set m to c & c
set d to text item delimiters
repeat while s contains m
set text item delimiters to m
set s to s's text items
set text item delimiters to c
set s to s as string
end repeat
set text item delimiters to d
s
end stripMultiples
set theString to "This has extra spaces.
I want them removed."
stripMultiples of space from theString
--> "This has extra spaces.
I want them removed."
-----------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden