Login

Perl: Concatenating Text and More

This marks the finale of our coverage of text in Perl (until we get into some more of the advanced Perl features at any rate). We left off with the here document and how to use it to display text exactly as we type it in, using a Mark Twain poem that he had written for his daughter’s tombstone (which interestingly enough was a rewrite of another poet’s poem). We also learned a little bit about ASCII and the values as they pertain to text, showcasing the 93 visible characters.

In this tutorial we are going to pick up on our discussion of ways to manipulate strings, starting with concatenation, or combining, and working our way through to converting strings. It’s a lot to cover in a short time, so let’s get to work.

Concatenation…The Function You Can Barely Spell

A lot of the string functions resemble mathematical equations. For instance, our first batter up is pretty similar to a simple addition. When you concatenate, all you are doing is adding two strings together. Here is an example:

#!/usr/bin/perl

print "My name is " . "whicka whicka " . "Mr. Sippi.";

This results in the following being displayed to your screen:

My name is whicka whicka Mr. Sippi.

You can also assign a variable to another variable through concatenation. In this next example, we are going to display the slightly repetitive lyrics of the Spaniels song, "Goodnight Sweetheart, Goodnight":

#!/usr/bin/perl

$a=’Goodnight sweetheart';

$b="Well, it’s time to go";

$c="n";

$d=’I hate to leave you, but I really must say';

$e= $a . ‘, goodnight';

print $a;

print $c . $b;

print $c . $a;

print $c . $b;

print $c . $d;

print $c . $e;

As you can see, the concatenation occurs with the variable $e, in which we add the string ‘, goodnight‘ to the variable $a. Afterward the variable $e holds the value: "Goodnight sweetheart, goodnight". This is the result of our program:

Goodnight sweetheart

Well, it’s time to go

Goodnight sweetheart

Well, it’s time to go

I hate to leave you, but I really must say

Goodnight sweetheart, goodnight

Note that when you have double quoted strings, you don’t always need to concatenate. Observe this sample:

#!/usr/bin/perl

$a=’Big ‘;

$b=’Macs';

print ‘I like to eat ‘ . $a . $b;

This prints out:

I like to eat Big Macs

If we had used double quotes, we could have accomplished the same thing like this:

#!/usr/bin/perl

$a=’Big ‘;

$b=’Macs';

print "I like to eat $a $b";

Printing this:

I like to eat Big Macs

without having to use the concatenating operator (.).

Remember that single quotes do not interpret, so had you tried that method with single quotes, like this:

#!/usr/bin/perl

$a=’Big ‘;

$b=’Macs';

print ‘I like to eat $a $b';

Your result would have been:

I like to eat $a $b

Which don’t taste anywhere near as good.

And lastly, if you want to concatenate a variable to another variable, you can use the concatenate assignment operator (.=), as in the following example:

#!/usr/bin/perl

$a=’Big ‘;

$b=’Macs';

$a .= $b;

print "I like to eat $a";

Which is really just shorthand for:

#!/usr/bin/perl

$a=’Big ‘;

$b=’Macs';

$a = $a . $b;

print "I like to eat $a";

It might not seem like a big difference, but when you begin adding a lot of variables to one another and get into more complicated coding, it really makes a difference.

{mospagebreak title=Making Copies}

If we want to repeat a string a bunch of times, we can do so by multiplying it. Let’s say I walk into a wrestling arena and am fighting the Badass Bulgarian. Here is a program that demonstrates what the crowd might say, if they know what is good for them:

James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne!

But what if we wanted to not have the words listed on the same line?

#!/usr/bin/perl

$chant="tUSA! n";

$chantTwo="tBulgaria blows! ";

$chantThree="James Payne! ";

print $chant x 10;

This prints the variable $chant 10 times, skipping a line and adding a tab at the beginning of each new line, resulting in the following:

USA!

USA!

USA!

USA!

USA!

USA!

USA!

USA!

USA!

USA!

{mospagebreak title=Chomping It Up Pac-Man Style}

If you want to chop off the end part of a string, you can do so using either the chop or chomp function. The chop function removes the last character of a string and returns what it chopped off, like so:

#!/usr/bin/perl

$scary="Look out it’s Lorena Bobbitt";

$a=chop($scary);

print $scary;

print "nOh God…I chopped off a $a! Now she’s gonna be pissed!";

This will print out:

Look out it’s Lorena Bobbit

Oh God…I chopped off a t! Now she’s gonna be pissed!

In the above example we assigned a value to the variable $scary, then used the chop function to chop off the last character and assign that removed character to the variable $a. We then printed $scary and a sentence with the chopped off character in it.

Chomp works in a similar fashion, and is said to be safer than using chop, as chomp removes two line-ending characters. Consider this code, which is the same as the above code, only I have added a newline escape character to it, to insert a space between text:

#!/usr/bin/perl

$scary="Look out it’s Lorena Bobbittn";

$a=chop($scary);

print $scary;

print "nOh God…I chopped off a $a! Now she’s gonna be pissed!";

This prints out:

Look out it’s Lorena Bobbitt

Oh God…I chopped off a

! Now she’s gonna be pissed!

As you can see, in this instance chop took our newline character instead of the t as we had intended. And instead of returning a t as I had wanted, it returned a newline, messing up the flow of my text. This is why it is safer to use chomp. Here it is in action:

#!/usr/bin/perl

$scary="Look out it’s Lorraina Bobbittn";

$a=chomp($scary);

print $scary;

print "nOh God…I chopped off a $a! Now she’s gonna be pissed!";

Tada! Note that if you have Perl version 4, chomp doesn’t work.

{mospagebreak title=Transformers…More than Meets the Eye}

There are a number of built-in Perl functions that allow you to do crazy things with text. Among those are ways to manipulate the case of characters, changing the first letter to upper/lower case, the whole string to upper/lower case, etc. Here they are in a program:

#!/usr/bin/perl

$thingOne="james";

$thingtwo="PAYNE";

print $thingOne . "n";

print ucfirst($thingOne);

print "n";

print uc($thingOne);

print "nn";

print $thingtwo;

print "n";

print lcfirst($thingtwo);

print "n";

print lc($thingtwo);

This will result in:

james

James

JAMES

PAYNE

pAYNE

payne

If functions aren’t your thing, you can also use special characters to achieve the same effect:

#!/usr/bin/perl

$thingOne="james";

$thingtwo="PAYNE";

print "l$thingtwo";

print "nn";

print "u$thingOne";

print "nn";

print "L$thingtwo IS MY LAST NAME E U$thingOneE is the first.";

This gives us the result:

pAyne

James

payne is my last name JAMES is the first.

A few things to note: the "l" and "u" act as lcfirst and ucfirst, respectively, while the "L" and "U" make everything following it uppercase until it reaches the "E", which signals an end to the special character.

Well unfortunately we did not get to cover every bit of string manipulation that I wanted to, but never fear: we’ll get to that in another article. Maybe even the next one, where I will also cover ways to manipulate numbers.