Lesson 3 – Super Perl

I'm Super Perl
And I'm here to save the world.


Let's fix up our "Pig Latin" translator from Lesson 2 a little bit more before we get sick of it. We'll read in a whole sentence and split it up into words:

print "Enter your sentence: ";
$sentence = <>;
chomp($sentence);
print "You're new sentence is: ";
@words = split(/ /, $sentence);
foreach $word (@words) {
    print substr($word, 1) . substr($word, 0, 1) . "ay ";
}
print "\n";
It's not perfect, but it demonstrates what you can do with a few lines of Perl.

Now let's take a look at what we added. First the split:

@words = split(/ /, $sentence);
The split command takes a string and splits it up into an array (a list, or an ordered set) of little component strings. How does it know where to split the string? Well, that's what the / / is for. It's called a delimiter . It tells Perl where to split and what to throw away. In this case, we split when we see a space, and we throw away the space, keeping just the bare words.

The reason that our delimiter is enclosed in forward-slashes instead of quotes is that it's a "regular expression." A regular expression is a way of describing what kind of string you are looking for. The regular expression / / stands for "just one space", because it contains just one space. Likewise, the regular expression /Sam/ stands for "Sam".  We'll see more useful regular expressions soon.

In our example, the sentence will be split at every space. Is this a good way to do it? Not really. What happens if somebody accidentally types in two spaces instead of one? A better way to do it would be like this:
@words = split(/ +/, $sentence);
This tells it to split at every group of contiguous spaces. The + means "at least one," so / +/ means "at least one space."

Next we see a loop:

foreach $word (@words) {
    print substr($word, 1) . substr($word, 0, 1) . "ay ";
}
We have an array of words called @words, and the foreach command loops through each element in the array and assigns it to $word. Then for each $word, it prints out the Pig Latin translation.  Quite simple.

OK, let's summarize what we've learned in this lesson.

  1. split - It takes a string and splits it up into its component parts.
  2. @words - It's an array of strings, which means that it contains a bunch of strings in a specific order.
  3. foreach $word (@words) - It puts each element of @word into $word and executes the loop.
Just for fun, let's put it all together into another example:

print "Enter your sentences: ";
$sentence = <>;
chomp($sentence);
@sentences = split(/(?<=[.!?])\s+/, $sentence);
foreach $sentence (@sentences) {
    print "$sentence\n";
}
print "\n";
In this example, we've used some regular expression syntax from Perl 5, so you should read about Perl 5 regular expressions here before you go on. Basically what we're doing is splitting at every group of spaces, but only if the spaces are preceded by punctuation.