Re: Arrays in AppleScript ?
Re: Arrays in AppleScript ?
- Subject: Re: Arrays in AppleScript ?
- From: kai <email@hidden>
- Date: Thu, 29 Dec 2005 22:26:19 +0000
On 29 Dec 2005, at 20:57, Adam Bell wrote:
On 12/29/05, kai <email@hidden> wrote:
On 29 Dec 2005, at 18:55, Paul Berkowitz wrote:
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
I suppose this is all a bit tongue-in-cheek, but you could also
traverse the list from the other end...
[snip]
But surely the "normal" way to do it would be to bite the list up
(after being sure that the insertion was interior to the list (not
done here):
set myList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
set listInsert to ASCII character 240 -- an apple
set insertAfterPosition to 7
insertInList(myList, listInsert, insertAfterPosition)
to insertInList(myList, listInsert, insertAfterPosition)
tell myList
set frontPart to items 1 thru insertAfterPosition
set backPart to items (insertAfterPosition + 1) thru -1
end tell
set myList to frontPart & listInsert & backPart
end insertInList
Sure, Adam. I was merely adding my 2¢ to those of Nick (alias Mr Tea)
and Paul (alias Mr Berkowitz), who were exploring ways of maintaining
the integrity of an initial list - without creating an entirely new one.
If you look again at my suggestion, you'll see that the result came
from the original list (myList):
InsertInList(mylist, listinsert, insertAfterPosition)
mylist
--> {"a", "b", "c", "d", "e", "f", "g", "x", "h", "i", "j"}
(Paul's routine could have included something similar.)
However, if you tried the same thing in your script, you'd get:
InsertInList(mylist, listinsert, insertAfterPosition)
mylist
--> {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
That's because a new list was created in your handler, and its data
is therefore no longer shared with that of the original. So, to
extract the data from the handler, you'd need to do something like:
----------
set mylist to InsertInList(mylist, listinsert, insertAfterPosition)
mylist
--> {"a", "b", "c", "d", "e", "f", "g", "", "h", "i", "j"}
----------
For more on data sharing, you might like to check out:
http://developer.apple.com/documentation/AppleScript/Conceptual/
AppleScriptLangGuide/AppleScript.9a.html
It's also worth bearing in mind that, if you use the old script
object referencing trick, modifying a list 'in place' can be
significantly faster than building a new one:
----------
on InsertInList1(theList, listinsert, insertAfterPosition)
script o
property l : theList
end script
set beginning of o's l to beginning of o's l
repeat with currPosition from 2 to (insertAfterPosition)
set item (currPosition) of o's l to item (currPosition + 1) of o's l
end repeat
set item (insertAfterPosition + 1) of o's l to listinsert
end InsertInList1
----------
---
kai
_______________________________________________
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