Re: Removing items from lists
Re: Removing items from lists
- Subject: Re: Removing items from lists
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 06 Oct 2002 08:17:41 -0700
On 10/6/02 7:16 AM, "Goldfish" <email@hidden> wrote:
>
What would be the correct syntax for removing an item from a list?
>
>
e.g. set x to every item of {"the","quick","brown","fox"} except "quick"
>
>
Any ideas?
There is no command to do it in AppleScript. (In an AppleScript Studio
document, I read "There is no command in AppleScript to remove list items
YET." Interesting, no?) You have to run a repeat loop. This handler gives
the idea. It's been generalized so you can exclude more than one item. If
you have only one item, put it in list brackets first. I.e.
set theList to {"the","quick","brown","fox"}
set exclItems to {"quick"}
set theList to my RemoveListItems(theList, exclItems)
to RemoveListItems(theList, exclItems) -- both lists
local tempList, theItem
set tempList to {}
repeat with i from 1 to (count theList)
set theItem to item i of theList
if {theItem} is not in exclItems then
set end of tempList to theItem
end if
end repeat
return tempList
end RemoveListItems
For reasons we can go into another time, if the main list theList is a large
list, over 100 items or so, it really pays to use the following version
incorporating a script object. it goes much faster. When your list is 1000
items or larger, it can be hundreds or thousands of times faster - a second
or so rather than minutes. Method thanks to Serge Belleudy-d'Espinose:
to RemoveListItems(theList, exclItems) -- both lists
local tempList, theItem
set tempList to {}
script listScript
property listRef : theList
end script
repeat with i from 1 to (count theList)
set theItem to item i of listScript's listRef
if {theItem} is not in exclItems then
set end of tempList to theItem
end if
end repeat
return tempList
end RemoveListItems
--
Paul Berkowitz
_______________________________________________
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.