Re: Wierd error
Re: Wierd error
- Subject: Re: Wierd error
- From: kai <email@hidden>
- Date: Wed, 24 Sep 2003 06:17:20 +0100
on Wed, 24 Sep 2003 02:50:17 +0100, Nigel Garvey wrote:
>
Paul Berkowitz wrote on Tue, 23 Sep 2003 08:05:15 -0700:
>
>
>You like typing quote marks? This will run much faster, since searching text
>
>("is in") is much, much faster than searching lists:
>
>
>
>my trim(" *%*&*!@#$^ |BLOW, JOE|* ^&*))&%^^%$ ")
>
>--> BLOW, JOE
>
>
>
>on trim(inputtext)
>
> set valids to
>
>"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
>
> repeat with i from 1 to (count inputtext) -- count is faster too, and no
>
>list
>
> if character i of inputtext is in valids then
>
> set startValid to i
>
> exit repeat
>
> end if
>
> end repeat
>
> repeat with j from 1 to (count inputtext)
>
> if character (-j) of inputtext is in valids then
>
> set endValid to j
>
> exit repeat
>
> end if
>
> end repeat
>
> set trimmedText to text startValid thru (-endValid) of inputtext
>
>end trim
>
>
And don't forget 'considering case', arranging the most likely characters
>
towards the beginning of the 'valids' string, and only counting
>
'inputtext' once. As a safety feature, the whole process could go in a
>
'try' block with the first repeat going up to (count inputtext) + 1. If i
>
gets that far, it means there are no valid characters in inputtext and
>
the attempt to test beyond the end of the string will cause an error.
>
'trimmedText' can then be set to "" in the 'on error' block.
All good stuff. It's also possible to use the start/end positions directly:
----------
to trim(t)
set v to
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set c to (count t) + 1
try
considering case
repeat with s from 1 to c
if t's item s is in v then exit repeat
end repeat
repeat with e from 1 to c
if t's item -e is in v then exit repeat
end repeat
end considering
t's text s thru -e
on error
""
end try
end trim
trim(" *%*&*!@#$^ |BLOW, JOE|* ^&*))&%^^%$ ")
--> "BLOW, JOE"
----------
I believe John F's original problem may have something to do with a bug
associated with certain types of encoded text:
----------
set text item delimiters to {""}
set t to "BLOW, JOE" as Unicode text
t's text items
--> {"", "", "L", "O", "W", ",", " ", "J", "O", "E"}
----------
However, if the encoded string is coerced to plain text:
----------
set text item delimiters to {""}
set t to "BLOW, JOE" as Unicode text
set {text:t} to t as string
t's text items
--> {"B", "L", "O", "W", ",", " ", "J", "O", "E"}
----------
---
kai
_______________________________________________
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.