Visual programing blocks – Lists

As in everyday speech, a Blockly list is an ordered collection of items, such as a "to do" list or a shopping list. Items in a list may be of any type, and the same value may appear more than once in a list.

List Creation

create empty list

The simplest list is the empty list, which is created with the create empty list block:

create list with

Basic use

The create list with block allows the user to specify the initial values in a new list. In this example, a list of words is created and placed in a variable named letters:

For this document, we'll denote this list as ["alpha", "beta", "gamma"], and we will refer below to the variables defined in this section.

This shows the creation of a list of numbers:

This creates a list of colours:

A list with values of different types is less common, but possible :

Changing the number of inputs

To change the number of inputs, click on the gear icon plus symbol. This opens a new window:

You can then drag item sub-blocks from the left side of the window into the if block on the right side to add a new input, as shown:

list-create-list-modify.gif

The new item was added to the bottom in this example, but it can be added at any position on the list. Similarly, unwanted item sub-blocks can be dragged from the if block to the left and removed.

create list with item

The create list with item block lets you create a list that has a certain number of copies of an item. For example, the following blocks set the variable words to the list containing ["very", "very", "very"].

Checking a list's length

is empty

The value of an is empty block is true if its input is the empty list and false if it is anything else (including a non-list). IS THIS TRUE? The value of the following blocks would be false because the variable colours is not empty: it has three items.

Note the similarity to the "is empty" block for text.

length of

The value of the length of block is the number of elements in the list used as an input. For example, the value of the following blocks would be 3 because colour has three items.

Note that the length of block tells you how many items are in the list, not how many different items are in it. For example, the following has the value 3, even though words consists of three copies of the same text (["very", "very", "very"]).

Note the similarity to the "length of" block for text.

Finding items in a list

These blocks find the position of an item in a list. For example, the following has a value of 1 because the first appearance of "very" is as the beginning of the words list (["very", "very", "very"]).

The result of the following is 3 because the last appearance of "very" in words is in position 3.

If the item is nowhere in the list, the result is the value 0, as in this example:

These blocks are analogous to the ones for finding letters in text.

Getting items from a list

Getting a single item

Recall the definition of the list colours:

The following block gets the colour blue because it is the second element in the list (counting from the beginning on the right):

The following gets green because it is the second element counting from from the end on the right:

The next block gets the first element, red:

The next gets the last element, yellow:

This block randomly selects an item from the list, returning any of red, blue, green or yellow with equal likelihood.

Getting and removing an item

A drop-down menu on the in list ... get block changes it to in list ... get and remove, which provides the same output but also modifies the original list:

This example sets the variable first letter to "alpha" and leaves letters as: ["beta", "gamma"].

Removing an item

Selecting "remove" on the drop-down menu causes the plug at the left of the block to disappear:

This removes the first item from letters.

Getting a sublist

The in list ... get sublist block is similar to the in list ... get block except that it extracts a sublist, rather than an individual item. There are several options how the start and end of the sublist can be specified:

In this example, a new list first letters is created. This new list has two elements: ["alpha", "beta"].

Note that this block does not modify the original list.

Adding items to a list

in list ... set

The in list ... set block replaces the item at a specified location in a list with a different item.

For the meaning of each of the drop-down menu options, see the previous section.

The following example does two things:

  1. The list words is created with 3 items: ["very", "very", "very"].
  2. The third item in the list is replaced by "good". The new value of words is ["very", "very", "good"].

in list ... insert at

The in list ... insert at block is obtained from the drop-down menu on the in list ... set block:

It inserts a new item into the list at the specified location, before the item previously at that location. The following example (built on an earlier one) does three things:

  1. The list words is created with 3 items: ["very", "very", "very"].
  2. The third item in the list is replaced by "good". The new value of words is ["very", "very", "good"].
  3. The word "you're" is inserted at the beginning of the list. The final value of words is ["You're", "very", "very", "good"].

Splitting strings and joining lists

make list from text

The make list from text block splits the given text into pieces using a delimiter:

In the above example, a new list is returned containing three items of text: "311", "555", and "2368".

make text from list

The make text from list block joins a list into a single text using a delimiter:

In the above example, a new text is returned with the value: "311-555-2368".

Related blocks

Printing a list

The print block in the Text category can display lists. The result of the following program is shown in the alert box:

Doing something for each item in a list

The for-each block in the Control category performs an operation on every item in a list. For example, these blocks individually print each item in the list:

This does not remove the items from the original list.

See also the examples of loop termination blocks.

Žádné komentáře
Zpět nahoru