Re: Removing characters from a string
Re: Removing characters from a string
- Subject: Re: Removing characters from a string
- From: Kai Edwards <email@hidden>
- Date: Sun, 03 Nov 2002 23:24:20 +0000
on 3/11/02 11:11 pm, I wrote:
>
I see that you also changed the last couple of lines of my earlier suggestion
>
but, to avoid falling foul of the message size limit, I'll respond to that
>
separately.
The change referred to involved these lines...
-------------------------------------------
set n to n as string
if n's length > 32 then set n to n's text 1 thru 32
-------------------------------------------
...which were replaced by a single line:
-------------------------------------------
if length of (n as string) is greater than 32 then [NO BREAK]
set n to (n as string)'s text 1 thru 32
-------------------------------------------
I'm generally keen to tighten up scripts wherever possible, but we also need
to take care not to 'throw out the baby with the bath water'.
For example, the above change raises a few interesting points:
* While the line count is halved, the character count increases by 25%.[1]
* The modified version involves a duplicated coercion[2].
* More importantly, the change breaks the conversion of a short name[3].
So, while I appreciate your efforts to refine my suggestion[4], the
following variation (this time in a handler) is probably about as far as I'd
personally want to take the process:
-------------------------------------------
set cleanName to clean("the name to clean")
to clean(n)
repeat with d in {",", ".", "/", ":", ""}
set {n, text item delimiters} to [NO BREAK]
{(n as string)'s text items, d's contents}
end repeat
if n's length < 33 then return n as string
(n as string)'s text 1 thru 32
end clean
-------------------------------------------
Sorry about the delayed response - and thanks for all your suggestions.
Best wishes.
Kai
===========================================
[1] Even if you prefer not to use the possessive form of syntax, the
one-liner's character count (sorry - the character count of the one-liner)
would still be slightly higher than that of the equivalent two-liner, mainly
because of [2].
[2] In the last line of the modified version, the coercion 'n as string' is
repeated. OMM, when coercing, say, a 40 item list to a string, this
repetition takes about 10% longer than the original two-liner. (Granted,
these coercions are pretty fast anyway, so the extra time may not be _too_
critical in this context. But every little helps...) :-)
[3] In the modified version, the variable 'n' is coerced to a final string
_only_ when its length is greater than 32. Otherwise, it's left as a list:
---------------------
set n to "short name"
-- do rest of script
n
--> {"s", "h", "o", "r", "t", " ", "n", "a", "m", "e"}
---------------------
[4] BTW, while we're doing some trimming, you might want to try running your
own initial suggestion (Tue, 29 Oct 2002 00:28:59 -0500) _without_ the three
lines involving text item delimiters. (You may find they're not entirely
essential to the operations involved there.) ;-)
--
email@hidden
email@hidden
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.