Removing an Item from a List (by value)

Материал из Поле цифровой дидактики
Описание Как удалить элемент из списка по значению этого элемента, а не по его индексу? В статье рассматривается пример на Scratch
Область знаний Информатика, Психология
Область использования (ISTE) Computational Thinker
Возрастная категория 9


Поясняющее видео
Близкие рецепту понятия
Среды и средства для приготовления рецепта: Scratch

Using the delete () of () list block, one can remove a specified item from a list based on its numerical value in the list, but there is no individual block that removes an item (or multiple items) which contains a specified string from a list. This tutorial will show how to remove an item from a list based on the string stored within the item.

Project Situations

There could be a number of purposes why one would need to remove an item from a list based on its value (or string). The following are possible situations:

  • A list contains of items found, and the items are in random order based on when they were discovered; 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 broken down as items are bought.

Code

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.


This second method removes all items that are the specified string 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.