Insert () at () of ()
insert () at () of [ v]
Category Variables
Type Stack
Introduced in 1.3

The Insert () at () of () block is a List block and a Stack block. 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.

Example Uses

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

If a list is used for holding objects in a special order and an object has to be added, using the Add () to () 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
ask [Where should I insert it?] and wait
insert [pie] at (answer) of [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 Add () to () block

Removed Features

Before Scratch 3.0, the block also had a dropdown with the options insert [thing] at (last v) of [list v] and insert [thing] at (random v) of [list v]. To use this in Scratch 3.0, the word "last" or "random" can be copied and pasted into the number field, or using the join () () block.

insert [thing] at (join [last] [] ) of [list v]

Workaround

This block can be replicated in a few ways. One way is to use two lists, with one list being copied to the other:

define insert (text) at (index) of list //run without screen refresh
delete all of [handler v] //this is a list used for adding to the other list
set [i v] to (0)
repeat (length of [list v]) //makes a copy
  change [i v] by (1)
  add (item (i) of [list v]) to [handler v]
end
delete all of [list v]
set [i v] to (0)
repeat (length of [handler v])
  change [i v] by (1)
  if <(index) = (i)> then
    add (text) to [list v]
  end
  add (item (i) of [handler v]) to [list v]
end

A workaround is also possible with just one list by recycling the list:


define insert (text) at (index) of list
set [i v] to (0)
repeat (length of [list v])
  change [i v] by (1)
  if <(index)=(i)> then
  add (text) to [list v]
  end
  add (item (1) of [list v]) to [list v]
  delete (1) of [list v]
end

See Also

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