Re: Slow script with long lists
Re: Slow script with long lists
- Subject: Re: Slow script with long lists
- From: Bastiaan Boertien <email@hidden>
- Date: Wed, 19 Oct 2005 11:29:07 +0200
What do you mean with very very slowly? I created a list with 50.000 items and it will delete an item in less than 2 seconds. I did it just like matt said but added one command
set theList to a reference to tr_list
set theList to ((items 1 thru (i-1) of theList) & (items (i+1) thru -1 of theList))
Another way is using applescript's text item delimiters to remove an item from a list. But I think this was not faster than matt did.
like this
x is the item to delete (integer)
on x, theList)
script myScript
property myList : theList
property myText : ""
end script
set AppleScript's text item delimiters to "<§:><§:>"
set myScript's myText to myScript's myList as string
set AppleScript's text item delimiters to ("<§:>" & item x of myScript's myList & "<§:>" as string)
set myScript's myList to every text item of myScript's myText
set AppleScript's text item delimiters to ""
set myScript's myText to myScript's myList as string
set AppleScript's text item delimiters to "<§:><§:>"
set myScript's myList to every text item of myScript's myText
set AppleScript's text item delimiters to ""
return myScript's myList
end removeItem
Op 19-okt-05 om 6:53 heeft Matt Deatherage het volgende geschreven:
On 10/18/05 at 10:42 PM, Chris Mahn <email@hidden> wrote:
The script below achieves this goal, although it does it very very
slowly when the list is only moderately long (>300 songs). I shudder to
think of it working on 20,000 songs.
Friends don't let friends do this kind of thing to lists. For a list of 300 songs, you're forcing AppleScript to find an item inside that list about 300 times, by copying 299 items and when setting "this_item" to "item i of trk_list". And then more for writing, and, oh, it just makes the head hurt. You're lucky it finishes at all.
To remove item "i" from your list, try this:
set dummyList to {}
set i to 55
set dummyList to ((items 1 thru (i - 1) of trk_list) as list) ¬
& (items (i + 1) thru -1 of trk_list) as list
This lets AppleScript create a list of items 1 through i-1, and another list of items i+1 through the end, and then concatenates them in one easy operation, instead of concatenating a new item to a list 300 times. On my system, this takes less than a second for a 4671-item list. The original takes a very long time.
You have to put parentheses around the (i+1) and (i-1) or AppleScript can't parse what you mean. There's probably a shorter way to say the same thing, but my syntax module is unloaded from my brain right now.
By the way, this line:
set trk_list to get index of every track of library playlist 1
..just produces a list of hundreds of items whose initial contents are the same as their indexes. Item 1 is the number "1", item 2 is "2", and item 4671 is "4671". I can see where you might want to do it for keeping track of every song in a random order, but if not, there have to be ways to do it without the overhead of 20,000-item lists.
--Matt
--
Matt Deatherage <email@hidden>
GCSF, Incorporated <http://www.macjournals.com>
Cheaters only prosper in Mario Kart.
Now Playing "Fifth Symphony: Moderatly and Sustained; Allegro Assai" by Otonowa Wind Symphonica, Alfred Reed, from "Alfred Reed Live! Volume 3: Giligia"
_______________________________________________
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
_______________________________________________
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