I’d like to know if it’s possible to remove an item from a list… I can’t get this done just using a list. I have to convert it into a string, use an osax to look for the item’s text, replace it with a “” caracter and reload as list the new string. Is there a better way?
: I’d like to know if it’s possible to remove an item from
: a list… I can’t get this done just using a list. I
: have to convert it into a string, use an osax to look
: for the item’s text, replace it with a “”
: caracter and reload as list the new string. Is there a
: better way?
You have to build another list. This isn’t as slow as it sounds, as it only involves a new set of pointers. The data themselves don’t need to be copied.
The simple method, which is good for removing every single instance of a value in a list is:
set newList to {}
repeat with thisItem in oldList
if contents of thisItem is not searchItem then
set the end of newList to thisItem
end if
end repeat
set oldList to newList
Slightly more involved, but faster - though only good for removing the first instance of a value in a list - is:
set newList to {}
set theCount to (count oldList)
repeat with i from 1 to theCount
if item i of oldList is searchItem then
if i > 1 then
set newList to items 1 thru (i - 1) of oldList
end if
if i < theCount then
set newList to newList & items (i + 1) thru -1 of oldList
end if
exit repeat
end if
end repeat
set oldList to newList
NG