1. Computing

Perl String Manipulation

How to use some of the most common string operators.

By

A Perl string is a sequence of characters, such as "The answer is 42". You can store as many characters in a string as you want; there is no real limit except the amount of memory in your computer.

You tell Perl where the string starts and ends with either single (' ') or double (" ") quotes.

Perl is great at manipulating strings. In this article we'll have a look some of the more common string operators.

Concatenation

'Concatenation' is a fancy word for joining strings together. The concatenation operator is represented as a period (.) It takes the string on its left and adds the string on its right and returns the new string.

For example:

$a= "I wandered lonely as a cloud,";
$b= " that floats on high o'er vales and hills";
$c=$a . $b;

$c now contains the new string "I wandered lonely as a cloud, that floats on high o'er vales and hills", while the original strings $a and $b remain unaffected.

Interpolation

Interpolation is another way to put strings together. It allows you to put a variable name within a string which will be substituted with the contents of that variable when the program runs and can be very useful.

For example:

$firstname= "William";
print "My favorite poet is $firstname Wordsworth";

In this example, Perl analyses the string and sees that there is a variable name within it. When it executes the print statement it will replace the variable name with the contents of that variable:

"My favorite poet is William Wordsworth"

Note that Perl only does this if the string is held within double quotes (""). If you use single quotes, Perl will output the string exactly as it is written. So, if you have any variable names or escape characters they won't be evaluated, just treated as part of the string.

For example, this script:

$firstname = "William";
print "My favorite poet is $firstname Wordsworth\n";
print 'My favorite poet is $firstname Wordsworth\n';
print "I am going to \n\n jump to another line.\n";
print 'I am going to \n\n jump to another line.\n';

Produces the following output:

My favorite poet is William Wordsworth
My favorite poet is $firstname Wordsworth\nI am going to

jump to another line.
I am going to \n\n jump to another line.\n

If you're using interpolation, and the variable name is surrounded by letters or numbers, Perl won't recognize it.

$day_of_week="Satur";
print "No work today, it is $day_of_weekday"; # returns an error

The way to get around this problem is to wrap the variable name in curly braces:

print "No work today, it is ${day_of_week}day"; # works!

Repetition

Another very handy operator is the repetition operator. Let's say you want to underline some output. You might be tempted to code it like this:

$underline = '--------------------';
print $underline;

But Perl lets you define a single character and then how many times you want to repeat it:

$underline = '-' x 20;
print $underline; # displays '--------------------'

Getting the length of a string

You'll often find it useful to know the length of a given string. Use the length() operator for this:

$dickens = "It was the best of times, it was the worst of times...";
$length = length($dickens);
print $length; # displays "54"

Yes - length() looks and behaves like a function, but it's actually an example of what Perl calls a 'named operator', which is basically an operator that uses a word instead of a symbol to make it more obvious what it does.

Converting Between Upper and Lower Case

Use the lc() and uc() operators to switch between upper and lower case:

$out1=lc("DON\'T SHOUT!"); # returns "don't shout"
$out2=uc("i\'m going to raise my voice!"); # returns "I'M GOING TO RAISE MY VOICE!"

This is just scratching the surface of the incredible support Perl has for working with strings. Don't forget the Perl quote word function and our guide to simple string matching with regular expressions.

  1. About.com
  2. Computing
  3. Perl
  4. Perl Tutorials
  5. Perl String Manipulation

©2014 About.com. All rights reserved.