A list (also called an array in other programming languages) is a tool that can be used to store multiple pieces of information at once. It can also be defined as a variable containing multiple other variables. A list consists of a numbers paired with items. Each item can be retrieved by its paired number. List blocks can be found in the Data blocks palette.
- Main article: List Blocks
The following blocks can be used to program lists:
add [thing] to [list v]— Adds an item to a list
delete (1 v) of [list v]— Deletes a chosen item of a list
insert [thing] at (1 v) of [list v]— Inserts an item at a chosen point in a list
replace item (1 v) of [list v] with [thing]— Replaces an item in a list with a new item
(list::list)— A reporter block with few uses; however, this can be used as a Stage Monitor
(item (1 v) of [list v])— A reporter block that reports what text an item in a list contains
(length of [list v])— A reporter block that reports how many items a list contains
<[list v] contains [thing]>— A boolean block that checks if a list contains a given string.
Items can be added to or deleted from a list manually or by programming. Holding down shift and pressing enter on a list results in a new entry above the previously selected entry, and pressing enter without the shift key creates a list entry below the previously selected one. They can also be added by right-clicking the list, clicking 'import', and selecting a plain .txt or Comma Separated Values file. Each line in the file will become a new item in the list.
They can also be exported in the same way; however, it is not possible to do so if the list has a name which is unsupported in the user's operating system. In Windows, lists using question marks, asterisks (*), angle brackets (< >), pipes (|), a colon (:), a space ( ), or any ASCII control character cannot be exported with Scratch. The first time the export button is pressed for this type of list, nothing will happen; following the second click, the import/export menu will close. The allowed characters will vary between operating systems.
Limits on list size
There is no limit to the length of an item or the number of items a list can hold, apart from an amount sufficient to crash Scratch. However, you may not be able to save a project online if your list takes too long to upload, because Scratch will return a network error after about 30 seconds. Few lists need to contain that amount of information, but a dictionary of 350,000 English words may fail to save when 250,000 words would succeed. This is due to the list being stored twice in the JSON source.
Some example uses for lists are as follows:
- Encoding/Decoding strings
- Artificial Intelligence
- Recording multiple values
- Conserving variables
- Saving and loading data
More Complex Uses
- Storing user-input data in Operating System simulations
- Storing AI data in games
- Allowing users to re-create past inputs
Copy and pasting
Because text in lists can be copied and pasted into text documents, and the data in lists can also be exported or imported, lists can be used to create save systems (along with the Ask () and Wait block).
- Main article: Cloud Data
Cloud lists were an experimental feature in alpha and beta versions of Scratch 2.0. However, they were disabled when Scratch 2.0 was released. The values of cloud lists were stored on the Scratch servers; this was the only difference between them and regular lists. When a cloud list got updated, it did so across all copies of the project. This makes cloud lists ideal for things like public high score lists. Many users wanted cloud lists to come back because it will make coding easier.
- Main article: Casting
Lists are cast to numbers and strings with a set of explicit rules depending on its contents.
The List Editor is a feature that allows you to edit lists. It is only avalible inside the project editor. Use it by clicking on a list item and typing in the new list item. You can also delete an item by clicking the "X" at the right and add an item by clicking the "+" at the bottom left of the list area.
The list editor can be used to make Programming languages in Scratch by importing a
*.txt file into the list. It is also useful when a project needs users to edit a list. The list editor is not as widely used as many other features such as Say and Ask. Many Scratchers do not know about the list; therefore, not many projects use it.
The list editor is also useful in text editors as creating a full text editor in Scratch is almost impossible because of the limitations of what characters can be used in the Key () Pressed? block. Text editors using list editors are easy to code as the project just has to access the list item.
The list editor has many limitations, which is why it is not widely used. It can only be used in the project editor, which means users can see all the code and edit it. Its use of Scratch features also means that it is hard on the user and is not customizable at all. List editors also do not have the best user interface, and it is often misleading for the user. Because of this, very few projects use the list editor and it is a very little-known feature.
- Fibonacci Generator by WeirdF
- FruitcraftRPG by Wodunne
- Random Level Generator by poopo
- Scratch Memory by spaceguy12
From the time lists were introduced, many users requested two new blocks,
hide list [list v] and
show list [list v]. Due to such high demand, they were included in Scratch 2.0.