Re: Deleting last item of a List
Re: Deleting last item of a List
- Subject: Re: Deleting last item of a List
- From: Hanaan Rosenthal <email@hidden>
- Date: Fri, 19 Nov 2004 16:21:55 -0500
The reason why using 'set end of' executes faster is because it creates a list with a slightly different use of memory.
The two ways to create a list are:
1. set theList to theList & newItem
2. set end of theList to newItem
Unless the item you are adding is a list by itself, the two options will create a seemingly identical list.
The lists differ, however, in the way they are handled in memory, and this is where speed comes in.
If you use method 1, the list is rebuilt and assigned a new place in memory every time you add an item. This makes the list take longer to be created, but it also makes the list faster to work with, since it is in one place in memory.
Method 2 will create a list faster, but it will be spread out in memory, and therefor will be slower to work with.
The ideal, I believe, is to create a list with method 2 and then use the copy command to copy it to another variable, therefore consolidating the list items to one place in memory:
set myTempList to {}
repeat...
set end of myTempList to ...--creates a list quickly
end repeat
copy myTempList to myList --makes trhe list faster to work with
Years ago, you could actually decide what type of list you want to create by using square brackets, which still works, but I am not sure what the implications are.
Try this for fun:
set myList to [1,2,3,4]
I am certain that using this tip would save you at least a second or two of script execution over your lifetime.
The lists and records chapter in my book covers this issue.
Hanaan
On Nov 19, 2004, at 4:00 PM, Nigel Garvey wrote:
set x to end of myList -- get the last item in the list
set x to last item of myList -- ditto
(For some reason, the former executes more quickly.)
--
"This would clearly be an outstanding choice (if not THE best choice)"
"Just plain excellent; the best out there. Hunker down with it and enjoy!"
"it's the best guide to AppleScript since the original Language Guide. And it brings everything up to date."
www.customflowsolutions.com/pages/applescriptbook.html
Got your copy?
_______________________________________________
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