Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fastest Method to Append to a List



Title: Re: Fastest Method to Append to a List
On 3/21/05 9:14 PM, "Mark Butcher" <email@hidden> wrote:


On 2005, Mar 21, , at 7:59 PM, Martin Fuhrer wrote:

I want to create a list of items, eg. a list of numbers from 5000 to -5000.  In Script Editor I can run the script:

set myList to {}
repeat with n from 1 to 10000
    set end of myList to 5000 - n  --SLOW!!
end repeat

Unfortunately, this is slow (about 30 seconds on my Powerbook G4 1.5 GHz).  A much faster method is:

set myList to {}
set myListRef to a reference to myList

repeat with n from 1 to 10000
    set end of myListRef to 5000 - n  --FAST!!
end repeat
  
This takes less than a second when run in Script Editor.   

Unfortunately, the second script will not work in an Applescript Studio project.  The loop produces the "NSCannotCreateScriptCommandError (10)".  The first script runs fine in Applescript Studio, but once again, takes about 30 seconds to complete.

If I recall correctly, 'my' worked for me:

repeat with n from 1 to 10000
    set end of my myList to 5000 - n  --FAST!!
end repeat

That won't work as is if myList was originally defined within a handler - as virtually all Studio variables are - since it's then a local variable by default and 'my' won't operate - you'll get an error.

The simplest thing to do is to just declare myList as

global myList

at the very top of your script, and then use 'my' as Mark says. (I can't imagine why that wouldn't be suitable, but alternatives would be to declare it as a

property myList : {} at the top

- which does the same thing - or to use instead a "Serge" script object within the handler:

script listScript
    property theList : myList
end script

and then refer to it as

    listScript's theList

everywhere within the handler thereafter. I can't see the advantage here, and some disadvantages if you need to refer the the list outside the handler.

--
Paul Berkowitz
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/email@hidden

This email sent to email@hidden

References: 
 >Re: Fastest Method to Append to a List (From: Mark Butcher <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.