Re: Arrays in AppleScript ?
Re: Arrays in AppleScript ?
- Subject: Re: Arrays in AppleScript ?
- From: Paul Berkowitz <email@hidden>
- Date: Thu, 29 Dec 2005 10:55:59 -0800
- Thread-topic: Arrays in AppleScript ?
On 12/29/05 10:07 AM, "Mr Tea" <email@hidden> wrote:
> This from has - dated 28/12/05 22.05:
>
>>> 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
>
>
> set mylist to {}
> set int to 1
> repeat 10 times
> set end of mylist to int
> set int to int + 1
> end repeat
>
> set listinsert to 7.5
> set end of mylist to 0
> set listcount to count mylist
> repeat 3 times
> set item listcount of mylist to item (listcount - 1) of mylist
> set listcount to listcount - 1
> end repeat
> set item listcount of mylist to listinsert
>
> mylist --> {1, 2, 3, 4, 5, 6, 7, 7.5, 8, 9, 10}
You've disguised a very good technique with some number games (the indices
of items starting out the same as their value) which makes it look too
specific to those games.
It can be generalized, so that you can set end of a list to some dummy
value, then change the values of all items beginning with the inserted item,
and all without making a new list. That's what I meant by "tricky" the first
time.
The first lines establishing the list, position and value are just an
example. The handler should work for all values chosen:
set mylist to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
set listInsert to "x"
set insertAfterPosition to 7
set mylist to my InsertInList(mylist, listInsert, insertAfterPosition)
--> {"a", "b", "c", "d", "e", "f", "g", "x", "h", "i", "j"}
on InsertInList(theList, listInsert, insertAfterPosition)
set end of theList to "dummy"
set listcount to count theList
repeat (listcount - insertAfterPosition - 1) times
set item listcount of theList to item (listcount - 1) of theList
set listcount to listcount - 1
end repeat
set item (insertAfterPosition + 1) of theList to listInsert
return theList
end InsertInList
--
Paul Berkowitz
_______________________________________________
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