Arrays and String Processing Algorithms
From Real Software Documentation
In this lesson, we will learn the basics of manipulating strings and how to use arrays. You will also see a simple example of an algorithm.
Say It Once
We are going to make a simple program in which the user types something and then it is displayed in a Message Box — but the program also tracks everything the user enters, and complains if they enter something twice.
- Create a new project. Add a Label, a TextField, and a PushButton to Window1 so it looks like this.
- Set the Title property of the Window to “Say Something”. Set the Name of the TextField to “Entry” and set the Text property of the Label to “Say Something:”.
- Click the Code Editor button (just below the Project tab) to switch to Window1’s Code Editor. Click the Add Property button in the Toolbar.
Real Studio will display the Property declaration area in the code editing area.
A property is a variable that belongs to an object. It is declared the same way a variable is declared in a Dim command. It automatically exists as long as the object exists. You can also set a default value for the property to have when the object first comes into existence (after the =). By clicking on the three buttons at the right side of the property declaration, you can set the scope of the property.
From left to right, these are Public (can be accessed from anywhere in the program), Protected (can only be accessed from code in the window you are looking at and any subclasses of this window — we will discuss subclasses in a later lesson) and Private (can only be accessed by code in this window). We will discuss why you might wish to limit the scope of the variable in this way in a later lesson. For now, change the scope to Public.
- Enter “Said()” as the name of the property and set its data type to “String”. Leave the last field empty.
The property name and its data type are mandatory but the default value is optional. Note that there are no spaces between “Said” and the opening parenthesis.
What we have just done is to declare an array. An array is a numbered list of data. Each item in the list must be of the same type (in this case, string).
The first element in the array is always numbered zero and when you are declaring the array, you must say what the highest-numbered element will be. Your code can then ask for the nth element’s value, or it can set that value, as long as that number is within the bounds of the array. You could declare the property as:
and then you’d have an array of 11 elements (starting at 0, and counting up to 10).
If we write:
we’d have an array with 1 element. So the way we ask Real Studio for an empty array is by leaving the upper bound blank.
By the way, you’d better get used to counting from 0. It happens a lot in computer programming.
Add a Function to the Window
- Click the Add Method button in the toolbar. Set the name of the new method to “AlreadySaid”, declare one parameter as “s as String”, and set the Return Type to “Boolean”.
The IDE should now look like this.
- Enter the following code into the Code Editor.//Pre: None
//Post: s is an element of Said, no other elements of Said are changed
// and if said was already an element, return true, else return false
Dim counter As Integer
//return true if s is an element of Said
For counter = 0 to UBound(Said)
If Said(counter) = s then
The Code Editor should now look like this.
It is helpful to put comments for people reading the code (including yourself). You should try to put enough comments to make the purpose of the method fairly apparent to someone who is reading that method for the first time.
A comment is written by putting two slashes // in a line. You can put anything you like after the slashes, and Real Studio will just ignore the text after the slashes You can put comments on their own lines or you can add them to the end of lines of executable code. If you do this, the code to the left of the slashes will execute and everything after the slashes will be treated as a comment.
You can also put as many comment lines in a row (each preceded by //) as you like. Although you can write very long lines, you might like to follow the example above, and break a single sentence or paragraph of comments into multiple comment lines.
There are two other ways to write comments, but you should avoid both of them:
- You can type the word REM instead of two slashes; this is just a holdover from older BASICs, and doesn’t stand out from the code as well as the two slashes;
- You can use an apostrophe ' instead of two slashes, but you should avoid this, because it is used in debugging (we will learn more about apostrophe comments in a later lesson).
Pre- and Post-Conditions
If you’re tackling a complex design, you should write Pre- and Post-conditions in your comments, if those conditions are not very obvious.
A pre-condition is something that must be true in order for the code in the method to work correctly. Often, you will design methods to be called at a particular point in the execution of the program, and you should describe what the state of the program should be like in a precondition comment.
A post-condition is something that’s guaranteed to be true after the method has finished running.
In principle, you should be able to prove that all the preconditions of every method in your program will be met, immediately before each method is called. And the post-conditions will help you to do that.
This may seem picky, but you will quickly find that writing large, complex programs without bugs in them is quite difficult[note 1]. If you think about proving to yourself that every method’s pre- and post-conditions are being met, at every step along the way, you will be helping to make your programs correct. You can also check the conditions yourself as you are debugging the program[note 2].
In the example above, we have no pre-condition, so it should be legal to run the code at any time. The post-condition tells someone looking at the code precisely what it does.
Return Statements Act as Exit Points
Notice that we are able to Return a value in the middle of running a For loop. This immediately terminates the loop, and the entire method. This means that if we get to the end of the For loop, we know we haven’t found what we were looking for, so then we Return false.
Accessing the Array
Notice how we access a particular element of the array: we put a numeric expression in parentheses after the name of the array:
That forms a larger expression whose value is, essentially, now a string variable. It’s a string variable, not just a string, because you can assign a value to it as well as find out its value, just as you can with any other variable.
We don’t directly assign to an array element in the code above, but we can say things like:
if we want to.
There are a number of ways to add elements to, or to remove elements from an array. In this case, we use the Append method of the array to add an element to the end of the array. Look in the on-line help to find other ways of adding and removing elements from an array.
would be perfectly valid (assuming there is at least one element in the array), but:
will always cause an error.
Note that since the first element of an array has an index of 0, the number of elements in an array is its UBound + 1.
Finish the program
- Add the following code to the Action event handler for the PushButton in the window:
//Post: alert the user if the entry has been entered before, store the entry so we can tell that.
if AlreadySaid (Entry.text) then
MsgBox "You already said that."
//Clear the entry after it's accepted.
Entry.text = ""
Run the Program
Now run the program, and observe what it does when you type in something twice. Try using the debugger to observe how the program works (don’t forget to step into the line that calls AlreadySaid). Also, once you’ve run AlreadySaid a couple of times, look at the Variables pane in the debugger, clicking on Self (to view the things in the window rather than the method you’re currently running), then click on the underlined link next to the variable Said so you can see the array’s contents.
You have to go through this because the debugger shows you where variables and properties live. This means you have to get at the properties of the window from the window’s variables pane. But the variables pane you first see when the debugger activates is the variables for the current method.
This business of Self vs. Me and how you access properties and variables in the debugger is a little complex. You might wish to read the Real Studio documentation if you would like more clarification on how it all works.
More on Arrays
In this program, we are treating an array like a mathematical set: it’s a box of things, and we just want to know if a particular thing is in it or not, and to be able to add things to it. The index (the number we provide to indicate which element of the array we want) is just a way of looking through those elements in order.
Try to think of how an array can be used in other ways. For example, an array can be used just like a function from a small range of integers to some value. This would execute very quickly. Many programs will pre-compute the value of some complicated function and store the values in an array, to avoid repeating a lengthy computation.
There are many things you could do from here:
- Modify the program to only remember the last, say, 5 things entered;
- Have the program report how many entries ago a repeat was entered;
- Have the program store and check only the first word entered.
- Write a program using an array to accept a series of numbers, then report their sum and mean (average). If your math is up to it, also do the same thing without the array, calculating the sum and mean as you go.