From Test-Scratch-Wiki
Using the 清单 积木, one can remove a specified item from a 清单 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 字串 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, you give one item 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
Programming
The following script can be used to program this. For this script, assume the following:
- "item#" is a 变量 used to iterate through the list until finding the desired item.
- the list's name is "gems".
- the item which is desired to be removed is the string "ruby".
![]() | this first method only removes the first instance of the specified string. The next method removes all instances of the specified string. |
變數 [item# v] 設為 (1) // so the process begins with the first item 如果 <清單 [gems v] 包含 [ruby] ?> 那麼 重複直到 <(清單第 (item#) 項項目\( [gems v] \) :: list) = [ruby]> 變數 [item# v] 改變 (1) // moves to the next item in the list to check if it is a "ruby" end // it will stop once it reaches the first item that is "ruby" 刪除第 (item#) 項 \( [gems v] \) end // if it does not, then the list cannot delete the item, and the script will not run further
This second method removes all items that are the specified string instead of just the first:
變數 [item# v] 設為 (1) // so the process begins at item 1 of "gems" 如果 <清單 [gems v] 包含 [ruby] ?> 那麼 重複直到 <(item#) > (length of [gems v] :: list)> 如果 <(清單第 (item#) 項項目\( [gems v] \) :: list) = [ruby]> 那麼 刪除第 (item#) 項 \( [gems v] \) // then remove that item 變數 [item# v] 改變 (1) // move to the next item end // if the current item is the specified string end // repeat until iterated through the entire list end // if the list at all contains "ruby"