This tutorial will show how to remove an item from a list knowing the value of the item (not its index).
Project Situations
There could be a number of purposes why one would need to remove an item from a list based on its value. The following are possible situations:
- A list representing an inventory of objects, ordered by when they were collected; later, one item is given away, and the list must remove that specified item based on its name from the list.
- A grocery list being reduced as items are bought.
One Item
The following script can be used to program this.
delete (item # of [ruby] in [gems v]) of [gems v]
This script will find the index of the first "ruby" and delete that item.
All Items
This second method removes all items that are the specified value instead of just the first:
repeat until <not <[gems v] contains [ruby]> > delete (item # of [ruby] in [gems v]) of [gems v] end
This script repeats deleting the first item that is "ruby" until it deletes all of the "ruby" items.
Alternatively one can loop over the items and delete them. This is more efficient when there are many items to be deleted:
define delete (value) from list
set [i v] to [1]
repeat (length of [gems v])
if <(item (i) of [gems v]) = (value)> then
delete (i) of [gems v]
else
change [i v] by (1)
end
end
| Running this in a custom block set to "run without screen refresh" will make it instant. |