Tweet

Using the Perl substr() function

Introduction

The substr() function is used to return a substring from the expression supplied as its 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

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $fragment =  substr $string, 4;
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

This script produces the following output:

    string: <Now is the time for all good people to come to the aid of their party>
  fragment: <is the time for all good people to come to the aid of their party>

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'.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $fragment =  substr $string, 7;
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

    string: <Now is the time for all good people to come to the aid of their party>
  fragment: <the time for all good people to come to the aid of their party>

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'.

  #!/usr/bin/perl
  use strict;
  use warnings;

  # Find the location of the substring 'people'

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $fragment =  substr $string, index($string, 'people');
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

    string: <Now is the time for all good people to come to the aid of their party>
  fragment: <people to come to the aid of their party>

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.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $length = 8;
  my $fragment =  substr $string, 7, $length;
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

Now we're starting at offset 7 and only returning $length bytes.

    string: <Now is the time for all good people to come to the aid of their party>
  fragment: <the time>

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'.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $length = 10;
  my $fragment =  substr $string, -16, $length;
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

    string: <Now is the time for all good people to come to the aid of their party>
  fragment: <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.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $length = -20;
  my $fragment =  substr $string, 7, $length;
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

    string: <Now is the time for all good people to come to the aid of their party>
  fragment: <the time for all good people to come to th>

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 its old-fashioned form.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  my $fragment =  substr $string, index($string, 'people'), 6, 'men';
  print "  string: <$string>\n";
  print "fragment: <$fragment>\n";

    string: <Now is the time for all good men to come to the aid of their party>
  fragment: <people>

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.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'Now is the time for all good people to come to the aid of their party';
  substr($string, index($string, 'people'), 6) = 'women';
  print "  string: <$string>\n";

    string: <Now is the time for all good women to come to the aid of their party>

See also

  perldoc -f substr
Revision: 1.5 [Top]