In my previous post, a simple program was shown how variables were applied to a moving Sprite using the “Up Arrow” and “Down Arrow” of the keyboard. In short, the value of a variable can generally be accessed or changed at any time by the programmer. However, such variable can only store one value at a time.
There are times when you need a variable to store multiple data in your script, Scratch’s “List” block provides that concept of arrays to help you with these circumstances.
The “List” block stores a series of values in a single variable. Each of these values is an 'element' of the “List” and each element has an index value to mark its position within the list. Suppose you would like to keep an inventory of the following grocery at home,
The “List” block will keep a record of the above elements, and is displayed by the "List Watcher" at the Stage Area :
Here, the order arrangement in the list corresponds to the sequence the data is entered and each of the data is indexed with a number accordingly.
For example, Mushroom is marked by an index value of “1”, Onion with an index value of “2” and so on. The index is referred to as “Item” in Scratch.
Note : I should have included the quantity for each element, but since the “List” block is a one dimensional array, I’ll leave it out for now until the next post where we will discuss about two dimensional arrays.
Start your Scratch programme now and click on the “Make a List” button (under “Variables” control block). A dialog box will appear as follows :
You are asked to give a name to the “List” you want to create, let’s name it “Grocery” and click the “”ok” button.
Once a list is created, an empty List Watcher will appear on the Stage Area, the control palette would look like below,
There are 7 blocks available in the “List”, and each has its own function. Let’s explore each block before we create a simple script for the Grocery.
1) “add () to ()” block
Drag the block to the Script Area and click twice on the block. You have just entered two identical elements (called “thing”) in the “Grocery” list and the contents are shown in the "List Watcher" below.
Replace the text with “Carrot” in the “add () to Grocery” block, click on it once and the list will be updated as shown below:
New entries always succeed the last item of the list.
2) “delete () of ()” block
I guess I don’t have to describe more, as your intuition should be able to tell you what the block does by its name. One thing you should know, the drop down menu displays only 3 items by default, regardless of its contents
There’s nothing to concern about, just key in the item number corresponding to the data you wish to delete.
The option “all” will remove all of the contents in the list.
3) “insert () at () of ()” block
This block is similar to the “add (thing) to Grocery” block, except that the new element “inserted” to the list precedes the item you had choosen. Referring to the list we had created earlier,
Let’s insert a new item “Onion” at item 2, and you will have a new list as below.
4) “replace item () of () with ()” block
This block changes (or replace) the specified item's content to a given entry.
Replacing Item 1 (currently "thing") with "Mushroom" yields the following
5) “item () of ()” block
This is a reporter block, reporting the content of its corresponding item in the list. It is commonly used in “Say” block or used for logical operation. Below is an example used in “Say” block
6) “length of ()“ block
This is a reporter block, which reports the total item a list contains. The total item is also displayed at the bottom of the List Watcher
7) “() contains ()“ block
This is a “Boolean” block, which returns “True” if the text entered is equal to any of the elements in the list, otherwise it returns “False”. For example, if you need to check whether “Apple” is in your Grocery List, it will return “False” as the current list contains only Mushroom, Onion, thing and Carrot.
Example script is shown below :
Note that the text is case-sensitive. Mushroom is not equal to mushroom, as far as the block is concerned.
Let’s start with an example how the “List” block is applied. We will use the same inventory, and is shown below for convenience.
We'll create a pre-defined inventory as follows :
Note 1 : Delete previous entries to avoid duplicates.
Note 2: Elements are written to the “List” block in sequence.
Note 3 : Used for linking to its corresponding elements.
Next, we’ll create a script to make the Sprite summarize (or "Say" out) your inventory :
Note 1 :
The Sprite does not know what’s in the grocery list. The “repeat until ()” block is needed to run through the whole list sequentially from the beginning to the last item. The variable “item” increments by 1 for every “repeat until ()” loop until it is greater than the “length of the grocery”, whereby the “repeat until ()” block exits its loop. Its process flow is shown below
Note 2 :
In the “if () else” block, it checks if “item” is greater than “length of Grocery”. If true, it does nothing and exits the block, else it “says” out the grocery name. The next event is taken over by the “repeat until ()” block, which repeats its process as explained in Note 1.
Note 3 :
The “say () for () secs” block allows only one specified text, and we need the Sprite to say out 4 different text, ie :
1) Name of the Grocery
2) “ located at “ text
3) “Item” text
4) Item number corresponding to the grocery
The “join () ()” block comes to the rescue, where it concatenates as many text as you wish.
Following steps show how “join () ()” block links the text together
Step 2 :
Step 3 :
Step 4 (final) :
The final "say ()" block will look like below :
Note that 3 different join () () ” blocks are used to concatenate the required text that we want.
The combination of “say () for () secs” and “join () () ” blocks results in the following
The whole script should look like below :
Below is the corresponding flow chart
Alternatively, you can create a script to make the Sprite ask for the groceries you would like to enter.Below is an example :
The above script allows you to enter as many data as you like until “x” is entered, telling the script you’re done with your entry.
The “ask () and wait” block stores your data to the “answer” block and pass it to the “if () else” block.
Note that there are two “if () else” blocks nested together within the “repeat until ()” block, the “else” statement of the first “if () else” block is itself an “if () else” block.
The first “if () else” block checks whether “x” is entered, if true, it exits the block. If false, it enters into the “else” statement. The second “if () else” block checks for “empty” data just in case if you had accidentally pressed the “Enter” key.
The flow chart below provides a better picture :
The new script is show below.
That's about it. Happy Scratching !!