Re: Convert a string into a list without Delimiters
Re: Convert a string into a list without Delimiters
- Subject: Re: Convert a string into a list without Delimiters
- From: has <email@hidden>
- Date: Sun, 11 Nov 2001 22:07:48 +0000
Christopher Stone wrote:
>
----------------------------------------------------------------------
>
set theText to "Now is the time for all good men to come to the aid of
>
their country."
>
>
set AppleScript's text item delimiters to {return}
>
set lst to text items of (REMatch theText pattern "....")
>
set AppleScript's text item delimiters to {""}
>
----------------------------------------------------------------------
This'll work better with a couple tweaks: a better choice of separator (in
case theText contains returns; I doubt it'll contain any ascii 1's) and a
more flexible pattern (can match last item if the count of the string is
not exactly divisible by 4):
set AppleScript's text item delimiters to ASCII character 1
set lst to text items of REMatch theText pattern "..?.?.?" separator ASCII
character 1
set AppleScript's text item delimiters to {""}
Might be quicker to keep the simple pattern, and get any leftover items
separately:
(count theText) mod 4
if result is not 0 then get items -(result) thru -1 of theText
Haven't compared them.
>
0.025797 sec to parse a 6 K string OMM. Timed with the PrecisionTimer osax.
Fast.:)
An alternative, using the Satimage osax:
change "(..?.?.?)" into ("\\1" & ascii character 1) in theText regexpflag
{"EXTENDED"} with regexp
You'll still need to chunk very long strings into, say, 6K blocks to avoid
the problems with turning strings into lists of >4065 items (discussed on
another thread).
Cheers,
has