What are lists?
To give you the short answer, lists are places where you can store information, just like variables. However, there is one key difference between lists and variables; variables can only store one piece of information at a time, whereas lists can store multiple bits of information at a time.
So, at any one point in time, a variable might only be able to store the number 10. A list, on the other hand, might be able to store the numbers 1, 2, 3, 4, 5 and 6.
In other words, if a variable is a container that can store one piece of information, a list is like one of those pillboxes that have multiple compartments for every day of the week.
Let's talk a bit about the way information is stored in a list. Let's think about a pill box: most of them are a series of small cubes that are joined together horizontally, right? Lists look much the same. The information is stored in a horizontal manner, with the first item in the list being the leftmost option, and the last item in the list being the rightmost option. We can compare this to the days of the week, where Monday is the first item in our list and Sunday is the last item in our list.
Each item in the list can also be referred to by its index. So, the first item in the list (aka Monday) is located at index one, the second item in the list (Tuesday) is located at index two, and so on.
How do you use lists?
Now let's move on to how to use lists in a program. Just like with variables, you can create a list in the Data category in the Scripts tab. Once you've created a list in your project, a bunch of list-related blocks will appear. Let's quickly run through some of the categories of blocks:
You can perform actions on your list. This includes:
· Adding, deleting, replacing or inserting items into your list
· Showing or hiding your list on the Stage
You have blocks that store information about your list.
· The item X of list_name block stores the value of the item located at index X.
· The length of list block stores the number of items you have in your list.
You also have a single Boolean block, which checks to see if your list contains some piece of specified information.
Let’s build a program
So now let’s build a program that uses lists. To make this program, first we need to create a list. Then, we want to generate five random numbers to store within that list. As you can see we use the add to block and the pick random block to generate and add numbers to the list. The program also checks to see if the number 19 appears anywhere in the list.
· If 19 does appear, Scratch Cat will say, "Number 19 is in the list."
· If 19 does not appear, Scratch Cat will say, "Today's not my lucky day…"
An if then block uses a Boolean statement to control whether or not the block should execute. If the statement is true, the instructions within the block will execute and if it’s not true, there will be no action. However, in this program we want Scratch Cat to say something when the Boolean statement is true and not true. This is when we use an if then else block. As you can see, this block tells Scratch Cat to say “Number 19 is in the list” if the statement is true, and “Today’s not my lucky day” if the statement is not true.
This code is inside a repeat block, as I’m sure you’ve noticed. This will save us having to copy and paste the same code 5 times, as this block tells the program to automatically repeat the instructions inside it the number of times we specify.
The last block I need to mention is a delete block. This block will execute whenever it’s reached in the program, so in this case, because it comes directly after the when flag clicked block, the data stored in the list will be cleared whenever we press the green flag. In other words, whenever we encounter this block any information we have previously generated will be removed.
By Jocelyn Glencross