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: ";It's not perfect, but it demonstrates what you can do with a few lines of Perl.
$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";
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.
@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) {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.
print substr($word, 1) . substr($word, 0, 1) . "ay ";
}
OK, let's summarize what we've learned in this lesson.
Just for fun, let's put it all together into another example:
- split - It takes a string and splits it up into its component parts.
- @words - It's an array of strings, which means that it contains a bunch of strings in a specific order.
- foreach $word (@words) - It puts each element of @word into $word and executes the loop.
print "Enter your sentences: ";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.
$sentence = <>;
chomp($sentence);
@sentences = split(/(?<=[.!?])\s+/, $sentence);
foreach $sentence (@sentences) {
print "$sentence\n";
}
print "\n";