Strings #2

#0 | #1 | #2 | #3 | #4 | #5

Continuing with our previous example of adding something to the end of a string, we left you with a little project - add a string to the beginning of another string.

This sounds like we could use the same technique, but with a bit of a change in the arguments to the substr function.

We know that the position of the first character of a string is '0', same as in a array. But a string is NOT an array of characters - we can't say $string[0] to refer to the first character in a string.

So using that, let's see if we can fudge, I mean, figure that out. Add this to '':

my $string3="01234567";
print "\n$string3\n";
substr($string3,0,0,"This is a string: ");
print "$string3\n\n";

Remember that $string2 was changed in our previous example, so we need to create a new one here - $string3.


Looking for a substring in an existing string is a very common activity, and Perl again makes this easy to achieve.

Using our $string3', how would we search for '345'? Using the index function. Remember strings are arrays of characters, and array items are 'indexed' starting at 0.

More typing for you - add this code to '':

print "\n$string3\n";
print "'345' found at position " . index($string3, "345") . "\n";

Found it!

We've snuck in another little string tool - notice the 2 '.' in the 2nd print statement.

In Perl a single '.' (dot) is the string concatenation operator. So we are printing 3 separate strings added together to make 1 string.

However it also means other things in other situations, but we can use it like this in string manipulations.

We will now show you how to find the length of a string - something very handy to do.

print "\nLength of '$string3' is: " . length($string3) . "\n";

I told you Perl makes easy things easy!

You have a few good string-handling functions to work with now, plus you should know a bit about arrays and loops.

Here is a little project that puts all these things together to show you how it might solve a 'typical' situation.

Our project is going to take a short list of cities and countries and capitalize each word. Next, print a list of the cities, followed by a list of the countries. Then we will print the whole list sorted by city, and again but sorted by country.

Ready? Here is the list:


Spend some time on this and see if you can get anywhere with it. In the mean time here is what my code produces:

To help me tackle projects I find it best to try to break the whole thing down into manageable steps. Otherwise it appears too big and overwhelming. Here are the steps needed to do this one:

  1. sort the list
  2. pull out cities and put in a separate array
  3. pull out countries and put in a separate array
  4. capitalize the first letter of each word
  5. print the sorted list of cities and countries

Your result will look different from mine, and how you code it will be as well. Just try to get one step working at a time. The important thing is to try. Good luck and try not to cheat.

Next page will have the code.

For more about sorting see Sorting #1.