Lists#
The same rules apply for lists as for variables regarding for all sprites and for this sprite only.
Declaration#
Operations#
Add item to list#
Delete item from list at index#
Delete all items from list#
Insert item at index in list#
Replace item at index in list#
Get item at index in list#
Get index of item in list#
TODO
Get length of list#
Check if item is in list#
Show/Hide List Monitor#
Get random/last item in list#
Compound Assignment#
Operator | Implementation |
---|---|
list_name[index]++; |
|
list_name[index]--; |
|
list_name[index] += y; |
|
list_name[index] -= y; |
|
list_name[index] *= y; |
|
list_name[index] /= y; |
|
list_name[index] //= y; |
|
list_name[index] %= y; |
|
list_name[index] &= y; |
List Data#
Initial data for lists can be stored inside the project. This behaves the same way loading text files into lists works in the Scratch editor. In addition to loading text files, you can also load data from various scripts and commands. This is useful for creating look-up tables or loading data from images or videos.
Loading data from text files#
Each line in the text file will be added to the list as a separate item.
Loading data from bash script#
The bash script enclosed in triple-backticks will be executed, and the standard output will be stored in the list, one item per line. The working directory will be set to the project directory.
Loading data from any other program#
The name of the program may be specified before the triple-backticks. This program will be executed with the standard input set to the script enclosed in the triple-backticks. The standard output will be stored in the list, one item per line. The working directory will be set to the project directory.
Any program that accepts input from stdin and outputs to stdout can be used.
For example, to load data from a python script:
Tip
If your script takes a long time to run, you can use bash to cache the output of the script.
Let's say that your script converts a file DEPENDENCY.txt
. You wish to only
re-run the script if the file DEPENDENCY.txt
has changed. We can use stat
to get the last modification time of the file.
Struct List Data#
If the list's type is a struct, each field will be filled with the value of the corresponding line in the data.
Example:
contents of file.txt
:
resulting in the following list: