Re: Trimming down a list
Re: Trimming down a list
- Subject: Re: Trimming down a list
- From: Steve Thompson <email@hidden>
- Date: Wed, 10 Apr 2002 16:58:46 +0100
on 10/4/02 4:22 pm, Paul Berkowitz at email@hidden wrote:
>
On 4/10/02 6:34 AM, "Steve Thompson" <email@hidden> wrote:
>
>
> I have this script
>
>
>
> set mrt to {long list of 10,000+ items created from a text file)
>
> set k to {}
>
> repeat with i from 1 to (the number of items in mrt)
>
> if item i of mrt is not in k then set k to k & (item i of mrt)
>
> end repeat
>
>
>
> ...the idea being that k contains a list of unique items based on whats in
>
> mrt
>
>
>
> Is there a quicker way to do this or an OSAX that will do it for me? I did
>
> find one in Akua sweets:
>
>
>
> set j to order list mrt with removal of duplicates
>
>
>
> But it takes longer than the script above!
>
>
>
> Any suggestion gratefully received,
>
>
If it's in main script (not a handler), then this should speed it up really
>
dramatically (suing 'my'):
>
>
set mrt to {long list of 10,000+ items created from a text file)
>
set k to {}
>
repeat with i from 1 to (count mrt)
>
set thisItem to item i of my mrt
>
if thisItem is not in k then set end of k to thisItem
>
end repeat
>
>
Aside from 'my' (which is even faster than 'a reference to' as outlined in
>
the AppleScript Language Guide for long lists) and is the main factor in
>
probably cutting the time here by (guessing) well over over 90%, 'count' is
>
faster than 'number of items'), and setting a variable 'this Item' means you
>
only have to get it once, not twice as you were doing with 'item i of k'
>
twice. That will cut the time by another 50% and is just good basic
>
AppleScript practice, which you should always do. Finally, setting the end
>
of a list to a new item is much faster than concatenating the list with the
>
item, and saves memory (and should have had the {item i of mrt} in list
>
brackets {} : I don't know how it even worked without that.)
>
>
Using all these techniques you will see an immense improvement.
>
>
>
The use of 'my' to reference a list was pointed out recently by Nigel Garvey
>
as equivalent to the use of a script object in a handler, as discovered by
>
Serge Belleudy-d'Espinoise. These techniques speed up iteration in list
>
repeat loops extremely, and the more so the bigger the list. In the case of
>
a list of 10,000 items you should see it speed up from what took several
>
minutes to a few seconds! In a handler, use Serge's technique:
>
>
set mrt to {long list of 10,000+ items created from a text file)
>
set k to my RemoveDuplicates(mrt)
>
>
on RemoveDuplicates(longList)
>
>
script RemoveDups
>
property listRef: longList
>
end script
>
>
set k to {}
>
repeat with i from 1 to (count longList)
>
set thisItem to item i of RemoveDups's listRef
>
if thisItem is not in k then set end of k to thisItem
>
end repeat
>
>
return k
>
>
end RemoveDuplicates
Now I'm really confused. Thank you for your example, Paul. I've created a
sub called RemoveDuplicates and I've passed my long list to it. The script
makes sense. The loop steps through each item of listRef to see if it's in
k. If it isn't it adds it to the end.
My sample file that I'm testing my script on contains 11,768 items. With
this script that should work as you've written it (ie, it makes sense to me)
I now get the first item in the list returned 11,768 times.
>
if thisItem is not in k then set end of k to thisItem
Makes sense to me. What's going on?
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.