Re: Arrays in AppleScript ?
Re: Arrays in AppleScript ?
- Subject: Re: Arrays in AppleScript ?
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 28 Dec 2005 17:49:07 -0500
On 12/28/05,
has <
email@hidden> wrote:
Incorrect. The standard AS list is a vector, which was introduced circa AS 1.1 IIRC. (The O(n) performance characteristics are due to badly designed safety checking.)
Really?? That seems like something that would be (relatively) easy to fix, high priority, and low impact (no change to the interface, just the performance). Since the problem is still around, I'm guessing that I'm wrong about at least one of those?
BTW, note that the term 'array' is often used very loosely; e.g. C array is a _very_ different beast to an NSMutableArray in terms of implementation and behaviour.
Whatever the heck an NSMutableArray is, sure. Pretty much any ordered list type whose elements are obtainable via random access by numeric index can be called an array. The term "list" usually has more specific meaning, but what that more specific meaning is varies dramatically from language to language. Perl documentation carefully distinguishes arrays and lists, for instance, but in Perl, all arrays are lists even though not all lists are arrays.
It would be odd to call Lisp lists arrays, although they meet the criteria above (thank you, nth function). Then there are Common Lisp vectors, which drive me nuts because if you're not careful you can't tell you have a vector on your hands; it appears to be a single atom equal to only the first element of the vector. Although I guess that's not much different from shell arrays in ksh and bash; for instance, after the assignment "a=(1 2 3)" (in bash, equivalent to ksh "set -A a 1 2 3"), you can get individual elements (
e.g. ${a[1]} == 2), or all the elements as a list ("${a[@]}"), or a single string containing all the elements separated by spaces ("${a[*]}"), but if you ask for just "$a", you get just "1" - as if the rest of the array didn't exist.
None of which has anything to do with AppleScript, so let's move on.
>What's a lot trickier in AppleScript than in some other languages is to insert a new element somewhere in the middle.
Not so much tricky as impossible; you have to create a brand new list by extracting a couple of sub-lists from the original list and concatenating these with the new element. 'Deleting' list items is similarly indirect and tedious.
Adding an element via concatenation does the same thing, does it not? e.g. "set a to a & {4}" creates a brand new list containing a copy of a plus the new element, which is then assigned as the new value of a. IIRC, that's why "set end of a to 4" is preferred, because it actually adds the new element to the existing list rather than creating a new one.
BTW, the List library on AppleMods <
http://applemods.sourceforge.net> contains ready-made handlers that will perform this and other basic list operations for you; saves having to roll your own.
Dang useful stuff. Oughta be in the core language if you ask me. :)
--
Mark J. Reed <
email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden