Perl Example #7
Working with Strings and Substrings

About the Program

This program demonstrates some of the string manipulation capabilities in Perl. It relies on the ability to determine the index of a substring using the functions index and rindex, as well as substr that can be used to replace that pattern with another string.
#!/usr/bin/perl

# Working with Strings and Substrings 

#  Using "length" to find the length of a string

$sentence = "Perl is great at manipulating strings, naturally.";
$len = length $sentence;
print "$sentence \n";
print "This string is $len characters long.\n\n";


Perl is great at manipulating strings, naturally.
This string is 49 characters long.
# Using "index" to find a substring  which returns the position
#  of some substring, or -1 if it is not found there.  

# Command Format:  $x = index ($bigstring, $littlestring);


$word = "Perl";
$where = index( $sentence, $word);
print "$sentence \n";
print "$word begins at character $where \n\n"; 

Perl is great at manipulating strings, naturally.
Perl begins at character 0

$word = "great";
$where = index( $sentence, $word);
print "$sentence \n";
print "$word begins at character $where \n\n"; 

Perl is great at manipulating strings, naturally.
great begins at character 8

$word = "xxx";
$where = index( $sentence, $word);
print "$sentence \n";
print "$word begins at character $where \n\n"; 

Perl is great at manipulating strings, naturally.
xxx begins at character -1

# Using "rindex" to find rightmost index

$word = "ing";

$where = index( $sentence, $word);
print "$sentence \n";
print "The first $word begins at character $where \n"; 

$where = rindex( $sentence, $word);
print "The last $word begins at character $where \n\n"; 

Perl is great at manipulating strings, naturally.
The first ing begins at character 26
The last ing begins at character 33

# Using the optional third parameter to "index" 
# Commmand Format:   $x = index($bigstring, $littlestring, $skip);
# Commmand Format:   $x = rindex($bigstring, $littlestring, $before);

$word = "at";
$first = index($sentence, $word);
$last = rindex($sentence, $word);
print "$sentence \n";
print "The index of the first $word is $first and the final index is $last\n";
$next = index( $sentence, $word, $first+1);
print "After $first characters, the index of the next $word is $next \n"; 
$previous = rindex( $sentence, $word, $last-1);
print "After $last characters, the index of the previous $word is $previous \n\n"; 


Perl is great at manipulating strings, naturally.
The index of the first at is 11 and the final index is 40
After 11 characters, the index of the next at is 14
After 40 characters, the index of the previous at is 24

# Extracting and Replacing Substrings
# Command Format:  $s = substr( $string, $start, $length);

# This grabs a substring

$grab = substr( $sentence, 5, 8);
print "$sentence \n";
print "Grabbed Pattern: $grab starts at 5 and goes 8 chars \n\n";

Perl is great at manipulating strings, naturally.
Grabbed Pattern: is great starts at 5 and goes 8 chars

# This replaces a substring
$replacement = "is totally awesome";
substr($sentence, 5, 8) = $replacement;
print "Substituting $replacement staring at 5 and going 8 chars \n"; 
print "$sentence \n\n";

Substituting is totally awesome staring at 5 and going 8 chars
Perl is totally awesome at manipulating strings, naturally.

The actual program: ex7.pl

The output: ex7.out

dhyatt@thor.tjhsst.edu