From Test-Scratch-Wiki

添加 () 为第 () 项
添加 [pie] 為第 ( ) 項\( [ v] \)
类别 清单类积木
形狀 条形积木
加入於 1.3

The Insert () at () of () 积木 is a 清单类积木 and a 条形积木. The block inserts an item containing the given text into the list, at the given position. All values below the inserted item are passed onto the item beneath them; the last item's value is placed in a new item at the end of the list.

用法示例

A visual representation of adding "beever" to position 3 in a list.

If a 清单 is used for holding objects in a special order and an object has to be added, using the 新增项目 () () block would ruin the sequence —this block, however, can insert objects without ruining the order.

Some common uses for the Insert () at () of () block:

  • Inserting items into a specific part of a list
詢問 [Where should I insert it?] 並等待
添加 [pie] 為第 (詢問的答案) 項\( [favorite foods v] \)
  • Word processors that record all characters in a list, where a character is being inserted in the middle of a sentence
  • Giving more precision than the 新增项目 () () block

应用

This block requires a long process but is indeed workaroundable. In the following example there is a general workaround of the "insert" block.

定義 insert (text) at (index) of (list) //run without screen refresh
刪除第 (全部 v) 項 \( [handler v] \) // this is a list used for adding to the other list
變數 [i v] 設為 (0)
重複 (字串長度\( (list) \)) 次 
  變數 [i v] 改變 (1)
  新增項目 (清單第 (i) 項項目\( (list) \) :: list) \( [handler v] \)
end // makes a copy
刪除第 (全部 v) 項 \( (list) \)
變數 [i v] 設為 (0)
重複 (清單 [handler v] 的項目數 :: list) 次 
  如果 <(i) = (index)> 那麼 
    新增項目 (text) \( (list) \)
  end
  新增項目 (清單第 (i) 項項目\( [handler v] \) :: list) \( (list) \)
end

参见

Cookies help us deliver our services. By using our services, you agree to our use of cookies.