home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Learning Perl

Learning PerlSearch this book
Previous: 14.7 Exercises Chapter 15 Next: 15.2 Extracting and Replacing a Substring

15. Other Data Transformation

15.1 Finding a Substring

Finding a substring depends on where you have lost it. If you happen to have lost it within a bigger string, you're in luck, because index can help you out. Here's how it looks:

$x = index(





Perl locates the first occurrence of substring within string , returning an integer location of the first character. The index value returned is zero-based; if the substring is found at the beginning of the string , you get a 0 . If it's one character later, you get a 1 , and so on. If the substring can't be found in string , you get -1 .

Take a look at these:

$where   = index("hello","e");                 # $where gets 1
$person  = "barney";
$where   = index("fred barney",$person);       # $where gets 5
@rockers = ("fred","barney");
$where   = index(join(" ",@rockers),$person);  # same thing

Notice that both the string being searched and the string being searched for can be a literal string, a scalar variable containing a string, or even an expression that has a string value. Here are some more examples:

$which = index("a very long string","long"); # $which gets 7
$which = index("a very long string","lame"); # $which gets -1

If the string contains the substring at more than one location, the index function returns the leftmost location. To find later locations, you can give index a third parameter. This parameter is the minimum value that will be returned by index , allowing you to look for the next occurrence of the substring that follows a selected position. It looks like this:

$x = index(







Here are some examples of how this third parameter works:

$where = index("hello world","l");   # returns 2 (first l)
$where = index("hello world","l",0); # same thing
$where = index("hello world","l",1); # still same
$where = index("hello world","l",3); # now returns 3
                        # (3 is the first place greater than or equal to 3)
$where = index("hello world","o",5); # returns 7 (second o)
$where = index("hello world","o",8); # returns -1 (none after 8)

Going the other way, you can scan from the right to get the rightmost occurrence using rindex . The return value is still the number of characters between the left end of the string and the start of the substring, as before, but you'll get the rightmost occurrence instead of the leftmost occurrence if there are more than one. The rindex function also takes a third parameter like index does, so that you can get an occurrence that is less than or equal to a selected position. Here are some examples of what you get:

$w = rindex("hello world","he");  # $w gets 0
$w = rindex("hello world","l");   # $w gets 9 (rightmost l)
$w = rindex("hello world","o");   # $w gets 7
$w = rindex("hello world","o ");  # now $w gets 4
$w = rindex("hello world","xx");  # $w gets -1 (not found)
$w = rindex("hello world","o",6); # $w gets 4 (first before 6)
$w = rindex("hello world","o",3); # $w gets -1 (not found before 3)