Re: Extracting from text [Lecture]
Re: Extracting from text [Lecture]
- Subject: Re: Extracting from text [Lecture]
- From: has <email@hidden>
- Date: Fri, 1 Mar 2002 00:23:41 +0000
Nigel Garvey wrote:
>
It's now generally well known amongst scripters that constructions like:
>
>
(characters 5 thru 137 of myText) as string
>
>
... can be horrendously wasteful in terms of the memory, processor
>
cycles, and typing required, and that the equivalent 'text' command is to
>
be preferred:
>
>
text 5 thru 137 of myText
>
>
The 'as string' construction creates an intermediate, 133-item list,
Just to be 100% precise for the viewers back home, it's the 'characters X
thru Y' construction that creates a list. The 'as string' part simply
coerces it back to a string again. (And yes, it is 'horribly wasteful',
though easy done if you're a novice.)
>
consisting of 133 single-character strings (double-character strings if
>
they're zero-terminated as they were on my Atari)
AS permits ASCII 0 in strings, therefore I doubt you have to worry about
that particular evil (blame C). This no longer being the Stone Age, I
imagine AS's string objects use a memory pointer plus a length value. That
would be the sensible approach.
>
and 133 pointers - each
>
of which is probably longer than the string to which it points. This list
>
is then immediately abandoned in favour of the string that's then derived
>
from it. The 'text' line, on the other hand, simply copies the derived
>
string directly from the original.
There's no need to duplicate a big chunk of memory simply to point to a
sub-section of that block, so I doubt there's any data copying done in
either situation. It's when you do stuff like concatenations that data gets
copied. [BTW, this always makes me wonder why some folk use ("" &
someString) instead of (someString as string) as the former is really bad
design and significantly slower.]
>
Although this is, as I've said, now well known, I've seen several
>
well-respected scripters over the past week give the following advice in
>
respect of path strings:
>
>
set AppleScript's text item delimiters to {":"}
>
set parentPath to (text items 1 thru -3 of folderPath) as string
>
>
This isn't perhaps as potentially calamitous as listing and then coercing
>
the individual characters of a large text, but its use does seem to
>
suggest that the authors don't know about the equivalent 'text'
>
construction, which is (in its simplest form):
>
>
text 1 thru text item -3 of folderPath
I spent quite a while darkly muttering "smug devil... too clever for his
own good... etc" while I trawled through the ASLG actually trying to find
mention of this feature. Which I _finally_ did, though it was less than
obvious and if you blinked you would miss it completely [not exactly an
uncommon occurrence in the ASLG, mind you]. Not at all in the same class of
sins, imho, though a bit of an unfortunate oversight, perhaps.
Still, thanks for the tip, Nigel. And now I think I'll step to the side for
a bit - I imagine there's a number of "well-respected scripters" would like
a word with you regarding what's left of their public image... [1] ;)
has (more of a Beeb man myself...)
[1] Or, as the great Douglas Adams put it: "... It startled him even more
when, just after he was awarded the Galactic Institute's Prize For Extreme
Cleverness, he got lynched by a rampaging mob of repectable Physicists who
had finally realised that the one thing they really couldn't stand was a
smartarse."
_______________________________________________
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.