Re: Text Item Delimiter error
Re: Text Item Delimiter error
- Subject: Re: Text Item Delimiter error
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 18 Mar 2001 00:30:04 -0800
On 3/17/01 10:09 PM, "email@hidden" <email@hidden> wrote:
>
I'm writing a search/replace handler and don't understand the results I'm
>
getting.
>
>
>
-----------------------
>
set someText to "_abcd_efg_hijk_lmnop_qrs_tuv_wx_yz_"
>
>
set thisSearch to "_"
>
>
set thisReplace to " "
>
--
>
--
>
set AppleScript's text item delimiters to thisSearch
>
>
set textItems to the text items in someText
>
--
>
*Case 1.a set searchedText to {}
>
*Case 2.a set searchedText to ""
>
>
repeat with iText from 1 to (count textItems)
>
>
set iTextItem to text item iText of someText
>
--
>
*Case 1.b set the end of searchedText to (iTextItem & thisReplace)
>
*Case 2.b set searchedText to (searchedText & iTextItem &
>
thisReplace)
>
>
end repeat
>
-----------------------
>
>
*Case 1.
>
searchedText is " _abcd _efg _hijk _lmnop _qrs _tuv _wx _yz _ "
>
--incorrect
>
>
*Case 2.
>
searchedText is " abcd efg hijk lmnop qrs tuv wx yz " --correct, but
>
----------
>
>
My first choice was Case 1, and I tried many variations of 'searchedText'
>
being a list but always got the same result. Won't Case 2.b start to eat up
>
memory when the text gets lengthy?
You're not seeing the power of text item delimiters just to substitute one
or the other and set the list of text items "as string". The text item
delimiter is just that: a text item _delimiter_ : what comes between text
items. So setting as list 'as string' puts the new delimiter between the
text items when setting them to a string. No concatenation at all is
involved, and consequently no memory problems. (Since 'delimiters' are
plural, "they" are put in {list braces} even though AppleScript currently
can only handle one delimiter. If you omit the braces, it still works
because the string "_", " " or whatever is coerced to its corresponding
single-item list.)
set someText to "_abcd_efg_hijk_lmnop_qrs_tuv_wx_yz_"
set thisSearch to "_"
set thisReplace to " "
set AppleScript's text item delimiters to {thisSearch}
set textItems to the text items in someText
set AppleScript's text item delimiters to {thisReplace}
set replacedText to textItems as string
set AppleScript's text item delimiters to {""} -- restore default
replacedText
-- " abcd efg hijk lmnop qrs tuv wx yz "
--
Paul Berkowitz