Re: Snippet to remove an item from a list
Re: Snippet to remove an item from a list
- Subject: Re: Snippet to remove an item from a list
- From: email@hidden
- Date: Mon, 9 Jul 2001 12:47:59 EDT
Victor,
Oh yes, it's been done before, and your handler will choke on very long
lists. With a list of 200 items, removing item #177, the following handler is
about 50 times faster. The error handling is failsafe for a CGI, and the code
is line wrap friendly.
on purgeFromList(theList, indexOfThePurged)
copy theList to safelist
try
set x to indexOfThePurged
if x < 1 then return theList
if x = 1 then return (rest of theList)
set coitl to length of theList
if x > coitl then return theList
if x = coitl then
set newlist to (items 1 thru -2 of theList)
else
set newlist1 to (items 1 thru (x - 1) of theList)
set newlist2 to (items (x + 1) thru -1 of theList)
set newlist to newlist1 & newlist2
end if
on error
copy safeList to newlist
end try
return newlist
end purgeFromList
In a message dated 7/8/01 8:50:07 PM, Victor Yee wrote:
>
Don't know if this one's been done before, but here's a snippet to remove an
item
>
from a list:
>
>
on deleFromList(thisListRef, thisIndex)
>
if (count of thisListRef) < thisIndex then
>
error "Can't remove non-existent item."
>
end if
>
repeat with i from thisIndex to 2 by -1
>
set item i of thisListRef to item (i - 1) of thisListRef
>
end repeat
>
set contents of thisListRef to rest of thisListRef
>
end deleFromList
>
>
set x to {"a", "b", "c", "d", "e"}
>
set xr to a reference to x
>
deleFromList(xr, 3)
>
x
>
--> {"a", "b", "d", "e"}
>
>
Victor