Re: Breaking up a long string
Re: Breaking up a long string
- Subject: Re: Breaking up a long string
- From: Nigel Garvey <email@hidden>
- Date: Thu, 19 Jun 2003 10:27:48 +0100
Joseph Weaks wrote on Wed, 18 Jun 2003 23:48:35 -0500:
>
Any suggestions on a better way to do this?
>
>
set thisParagraph to "A really long paragraph that I need to break into
>
longer chunks..."
[!]
>
set maxParagraphLength to 400
>
>
repeat with x from 1 to (length of thisParagraph) by maxParagraphLength
>
try
>
set thisChunk to (characters x thru (x + maxParagraphLength)) of
>
thisParagraph as string
>
on error
>
set thisChunk to (characters x thru (length of eachParagraph)) of
>
thisParagraph as string
>
end try
>
>
-- do stuff with thisChunk
>
>
end repeat
set maxParagraphLength to 400
set thisParagraphLength to (count thisParagraph)
repeat with x from 1 to thisParagraphLength by maxParagraphLength
set y to x + maxParagraphLength - 1
if y > thisParagraphLength then set y to thisParagraphLength
set thisChunk to text x thru y of thisParagraph
-- do stuff with thisChunk
end repeat
>
P.S. The "as string" was causing the app that calls this script to crash.
>
It took me awhile to find this culprit, which was happening because I had
>
earlier changed Applescript's tid WITHOUT restoring previous value.
Another culprit will eventually be your use of the '(characters x thru y
of z) as string' syntax. This creates a list of individual characters and
then creates the string you want, which uses a hell of a lot of memory -
especially with repeated actions on long texts. You can dispense with the
list altogether by using 'text x thru y of z', which extracts the
substring directly from the longer string. This saves memory and
execution time and - since no list is coerced to string - is unaffected
by the state of the TID's.
NG
_______________________________________________
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.