Using the Perl substr() function
Introduction
The substr()
function is used to return a substring from the expression
supplied as it’s first argument. The function is a good illustration of some
of the ways in which Perl is different from other languages you may have used.
substr()
has a variable number of arguments, it can be told to start at an offset
from either end of the expression, you can supply a replacement string so
that it replaces part of the expression as well as returning it, and it can
be assigned to!
Example 1a. Supply an expression and a positive offset value.
In our first example, we’ll use $string
as the first argument, or
expression, and supply the offset argument to indicate how far from the left
side of the expression our substring should start. Since substr()
returns the substring that it matches, we assign this value to $fragment
This script produces the following output:
In $string
, the ‘i’ in the word ‘is’ is at offset four, because the function starts
counting at zero. Therefore the letter ‘N’ is at offset 0, ‘o’ is at offset 1, ‘w’ is
at offset 2, and so on.
Example 1b. Supply an expression and a positive offset value.
If we increase the offset to 7, the substring return by substr()
starts at the word
‘the’.
Example 1c. Supply an expression and a positive offset value.
In this example, instead of an explicit offset argument, we’ll use the
index()
function to determine the position of the word ‘people’.
Example 2a. Supply an expression, a positive offset value and a length
We can supply a third argument to substr()
, to limit the size of the
substring that is returned. This is the length argument.
Now we’re starting at offset 7 and only returning $length
bytes.
Example 2b. Supply an expression, a negative offset value and a length
If we supply a negative offset value, then substr()
starts that many
characters from the end of the string. Here our offset is -16. which means
that our substring will start at the word ‘aid’, and since our length argument
is 10, the function returns the string ‘d of their’.
Example 2c. Supply an expression, a positive offset value and a negative length
If we supply a negative length argument, then that many characters are
truncated off the end of the string.
Supplying negative arguments might seem weird, but that’s OK, because Perl is
weird. These negative arguments save you the bother of having to work out
where the end of the string is. (And that can be very useful).
Example 3. An expression, an offset value, a length and a replacement value
It’s time to confess something. The string we’ve been using in our examples
is an old typing exercise. The original typist’s example assumed that only
good men could come to the aid of their party! So let’s supply the word ‘men’
as the fourth argument, and see that substr()
then modifies $string
,
returning it to it’s old-fashioned form.
As you can see, our call to substr()
has replaced the word ‘people’ with the
word ‘men’. By supplying a fourth argument, we have modified the expression
$string
. At the same time, substr()
still returns the substring ‘people’.
Example 4. Assigning to substr()
It is perhaps more common to see the modification demonstrated in the previous
example achieved by direct assignment. In technical terms, this means that
the substr()
function can be used as an lvalue, something that can be
assigned a value.