Re: Removing items from a list
Re: Removing items from a list
- Subject: Re: Removing items from a list
- From: "John C. Welch" <email@hidden>
- Date: Wed, 10 Mar 2004 10:23:14 -0600
On 3/6/04 4:35 PM, "Steve Meyer" <email@hidden> wrote:
>
Using recursion and the remarkable "rest of" command, and directly
>
plagiarizing Matt Neuburg and AS:The Definitive Guide (page 153):
>
>
>
set x to {"fu", "foo", "bar", "fu", "foobar", "fubar"}
>
set z to {"fu", "foobar"}
>
>
on deleteItems(theSet, DeleteThese)
>
if theSet = {} then return theSet
>
if item 1 of theSet is in DeleteThese then
>
return deleteItems((rest of theSet), DeleteThese)
>
else
>
return {item 1 of theSet} & deleteItems((rest of theSet), DeleteThese)
>
end if
>
end deleteItems
>
set y to deleteItems(x, z)
>
y
>
--> {"foo", "bar", "fubar"}
On a related note, if you're just trying to remove single items from a list,
I rigged this one up. (Matt's works really well too, but debugging recursion
makes my head hurt. Hell FOLLOWING recursion makes my head hurt.) I don't
know which would be faster, but this doesn't seem to be terribly slow per
run.
on deleteItemsFromAList(listBeingModified, indexToBeDeleted)
--list is a list, index is an integer value
if listBeingModified is {} then --zero items in the list, who cares
return listBeingModified
else if (length of listBeingModified) = 1 then
--one item in the list, clear it and be done
return {}
else if indexToBeDeleted = 1 then --deleting the first item in the list
return rest of listBeingModified --return the rest of the list
else if indexToBeDeleted = (length of listBeingModified) then
--deleteing the last item in the list
set listBeingModified to reverse of listBeingModified
--reverse list order
set listBeingModified to rest of listBeingModified
--remove new item 1
return reverse of listBeingModified
--reverse back to original order and return
else --delete item in some arbitrary position
return (items 1 through (item (indexToBeDeleted - 1)) of
listBeingModified) & (items (indexToBeDeleted + 1) through -1 of
listBeingModified)
--yank the offending item and return it
end if
end deleteItemsFromAList
That final else statement is one line plus a comment.
Matt's is probably more elegant, but I can read mine easier ;-)
john
--
"What is this talk of 'release'? Klingons do not make software 'releases'.
Our software 'escapes' leaving a bloody trail of designers and quality
assurance people in its wake."
- 8th most commonly uttered Klingon programmer phrase
_______________________________________________
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.