Scratch Custom String Blocks

Having the ability to manipulate strings is one of the most powerful facilities you can have in a programming language. In my day to day coding I manipulate strings an awful lot. You may want to be able to get specific characters in a string, convert the whole string to uppercase characters or lowercase characters. Many years ago, one of the first programming languages I learnt was BASIC (http://en.wikipedia.org/wiki/BASIC). BASIC has several functions for allowing you to manipulate strings, for example:

LEFT$(string, n)

LEFT$ allows you to return the LEFT most n characters from your string. For example if you use the command PRINT LEFT$(“Scratch”,3) the letters Scr would be printed to the screen. The n parameter tells the function how many characters starting at the beginning of the string you want.

RIGHT$(string, n)

Similar to LEFT$, RIGHT$ allows you to return the RIGHT most n characters from your string. So something like PRINT RIGHT$(“Scratch”, 3) would print tch to the screen. The n parameter tells the function how many characters starting at the end of the string and working backwards you want.

MID$(string, start, n)

MID$ is quite a powerful function. It allows you to get a certain amount of characters, but starting at any point in the string. So using a command like PRINT MID$(“Scratch Programming”, 9, 7) would print Program to the screen. The start parameter tells the function where you want to start in the string and the n parameter tells the function how many characters you want to return from the starting point.

UPPER$ and LOWER$

Some other functions available are UPPER$ and LOWER$. These return either an uppercase or lowercase version of your string. So something like PRINT UPPER$(“scratch”) would print SCRATCH and PRINT LOWER$(“SCRATCH”) would print scratch. Again, very useful!

How can we do this in Scratch?

Out of the box, Scratch does not have these kind of blocks built in. As we know, Scratch 2 allows us to write our own custom blocks, so we could write our own versions of these. What I have done is build versions of the functions explained above as custom blocks that can be used in your Scratch programs.

I have made these available at http://scratch.mit.edu/projects/12402145/. If you look inside the project, all the custom blocks are defined on the stage, with an example of how you would use each block next to the block definition. I will given an explanation below on how you would use each custom block.

Please Note: The resulting string from each of these blocks is stored in a variable called ‘result’.

Scratch LEFT$

This has been implemented as a custom block called Left. You would use the block like this:

Scratch Left BlockThe first parameter is the string or the variable holding the string you want to work with. The second parameter is the number of characters from the left you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold Scr.

Scratch RIGHT$

This has been implemented as a custom block called Right. You would use the block like this:

Scratch Right BlockThe first parameter is the string or variable holding the string you want to work with. The second parameter is the number of characters from the right that you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold tch.

Scratch MID$

This has been implemented as a custom block called Mid. You would use the block like this:

Scratch Mid BlockWe have three parameters that we have to supply to this block. The first parameter is the string or variable holding the string you want to work with. The second parameter is where you want to start in the string. The final parameter is the number of characters you want to get from the string. The result is stored in a variable called result. So, looking at the example call above, we can see that we will be working with the string Scratch Programming and we want to start returning characters from the 9th character in the string and that we want to return 7 characters. Starting at the 9th character (P) we will get 7 characters, which will give us the string Program.

Scratch UPPER$ and LOWER$

I have also implemented blocks that allow strings to be converted to upper of lower case. These blocks are called ToUpperCase and ToLowerCase. As with all the other blocks, the result is stored in a variabled called result. You would use the blocks like this:

Scratch ToUpperCase BlockScratch ToLowerCase BlockCalling the ToUpperCase block on the string shown would convert it to SCRATCH and calling the ToLowerCase block on the string shown would convert it to scratch. If you supplied a string such as ScRaTcH to either of these blocks, it will still work as expected.

I am going to write a further blog post on how the ToUpperCase and ToLowerCase blocks work, because Scratch is a little different to most languages when comparing upper and lower case characters. The code for the other blocks is fairly easy to follow and figure out whats going on.

Anymore Blocks?

I will be implementing more string manipulation blocks over a period of time. The blocks I want to implement next are:

  – SPLIT(string, char)
  – LTRIM(string)
  – RTRIM(string)
  – TRIM(string)

SPLIT enables you to split one string into several strings, splitting the string at a specific character. For example if you supplied a string I Program In Scratch and told the block to split on the space character, you would end up with 4 strings containing:

  I
  Program
  In
  Scratch

Each of these strings would be held in a list.

LTRIM and RTRIM removes all spaces to either the left or the right of a string. So if you had a string ”    Scratch” (quotes used to show the spaces) and passed the string to the LTRIM block, it would remove all the leading spaces. Similarly, if you had the string “Scratch   ” and passed the string to the RTRIM block, it would remove all the trailing spaces. TRIM performs a union of LTRIM and RTRIM and will remove all leading and trailing spaces.

As these blocks are added, I will add a post to this blog and will also add a comment in the Scratch forums and on the ScratchEd web site.

Use and Share!

I do hope that you find these blocks useful and feel free to use and modify them in any way you would like. Share them around with your fellow Scratchers.

Lists in Scratch 2

Scratch ListsVariables in Scratch allow us to store a single piece of data that we can use in our scripts. This blog post will introduce Lists (also known as arrays in other programming languages). Where variables allow us to store a single piece of information, lists allow us to store multiple pieces of information in the same way we would make a list of items on a sheet of paper. One of the most common lists we make in our lives is a shopping list, and I will use the idea of a shopping list to explain the concept of Scratch lists.

When we write a list on paper, we very often add items to the list from top to bottom, i.e. we add item 1, then item 2, etc. We can do this with lists in Scratch, and we can also insert items at a specific location in the list, remove items from a list, etc. In this post we will cover all the actions you can perform on a list. Code blocks will also be given to show exactly how you express this in the Scratch environment.

Creating a List

To create a list in Scratch, from the Block palette select Data and then click the Make a List button, give your list a name and then click OK, an empty list is then displayed on the Stage. At this point you are then ready to start adding items to your list. In all the example code blocks in this post, the list will be called ShoppingList.

Adding Items

To add an item to our list, we use the add block, which looks like:

Add Block
Where we see the word thing, we replace this with the word, number or sentence we want to add to the list. Looking at the picture of the list at the beginning of this post, we can see the first item in the list is Apples, so we replace thing with Apples, and we then have our first item in the list. If we then add another item, say Oranges, you will see that this is added to the bottom of the list. As we keep adding, the list grows. In programming terms, a list is dynamic in size, meaning it is not a fixed length.

As we are building a shopping list, we want to be able to add many items to it. The code block below allows this to happen. All the code is doing is repeating a block of code asking the user what they want to add to the list. If the user enters the word finish the code block stops.

Adding to a ListInserting Items

So, as we can see its very easy to add items to the end of a list. What about if we want to insert an item within our list? Well this is very easy to do by using the insert block. The insert block allows us to specify what we want to insert and the position in the list where we would like to insert it. If you click on the drop down on the insert block, you will see by default the options 1, last and random. Selecting 1 will insert the item at position 1 in the list, moving the current items in the list down by 1 position (so the current item 1 becomes item 2, etc), last will insert at the end of the list (the same as adding) and random will insert into a random position in the list. We are able to specify the exact position in the list we would like to insert our item. The code block below shows an example of inserting into a list.

List Insert
We simply specify what we would like to insert into the list and the position we would like to insert to. So, using the example of our list at the beginning of this post, if we inserted Milk at position 3, our list would then look like:

1. Apples
2. Oranges
3. Milk
4. Grapes
5. Bread
6. Cheese

As you can see, everything that was in position 3 onwards in the list has now moved down one position.

Deleting from a List

Deleting, or removing items from a list is achieved by using the delete block. You specify the item number you want to delete from the list. By default, the drop down in the block contains 1, last and all, but you can replace this with your required item number. the option all will delete every item from your list (essentially emptying the list), and this is something I would recommend that you do before at the beginning of your scripts. Generally you will always want your scripts to work with an empty list. The code block below shows how to delete an item from a list.

List DeleteReplacing items in a List

A nice feature provided in Scratch is the ability to replace an item in a list with something else. This is basically the same as performing the following two things:

1. Remove an item at a specific position
2. Insert an item at a specific position

The replace does this for us using the replace block. We simply provide the replace block with the location of the item in the list we wish to replace, and the item to replace it with. For example, again if we use our list given at the beginning of this post and we could replace Oranges at position 2 with Pears by simply using the block:

Replace Block
A slightly more complete script block could be as follows:

List ReplaceWhat else can we do with a List?

So, we have covered Inserting, Adding, deleting and Replacing items in a list, what else can we do? There are three more important things we can do with a list, these are:

1. Get an item at a specific position
2. Get the length of a list
3. Search a list for an item

Again, as with all the other list functions, there are blocks that allow us to do the three things. Lets have a look at these in order.

Getting an item at a specific position

Say we want to display an item at a specific position in the list, we use the item block. With this block, we specify the position of the item we want to return and this can then be stored in a variable or you can display it, etc. For our example we will display item number 3 (Grapes in our original list). To do this, we can use the following code block:

List ItemGetting the length of a list

As I’ve already mentioned, the size of a list is dynamic, meaning that the more you add, the bigger it becomes and there will come a time when you want to find out just how long your list is. You can find out the length of your list by using the length of block. In the same way you can return an item in a list to a variable, you can return the length of your list to a variable or again, display it. So, to display the length of our shopping list, we could use the following code block:

Length OfSearch a list for an item

Finally, we can search a list for an item. As you can imagine this is a very useful thing to be able to do. You can do this by using the contains block. The code block below, shows an very simple example of searching for an item in your list.

List ContainsConclusion

This has been a brief introduction to using lists in Scratch. I do hope you find it useful. Feel free to comment on this, use it in your teaching or any way you see fit.

I have produced a very simple Scratch project that was built to help write this post. The link for this is http://scratch.mit.edu/projects/11152013/