Perl index Function

The index function is used to return the position of the first occurrence of a substring in a string.

You can use the Perl index function to find a substring within another bigger string. You can use the regular expression to match the first instance of a substring in a string but this function may be faster and easier to understand.

The syntax forms of the index function are as follows:

index STR,SUBSTR,POSITION
index STR,SUBSTR
where the argument parameters have the following significance:
  • STR - the string you are searching
  • SUBSTR - the substring whose position you want to locate or to search for
  • POSITION – the position in the string from where you want to start the searching or the number of characters to skip over before searching for the substring

This function will return -1 if the substring will not be found or the position of the first occurrence of the substring, otherwise.

Please remember that the first character of a string has the position 0, the next 1, and so on.

If you don’t supply the third argument, i.e POSITION, that means that the index function will search for your substring beginning with the first character of the string. 

You can use the index function to check if a string is a substring of another.

The index function returns -1 if the substring is not found.

You can use the index function in a conditional test, by testing if the returning value is greater or equal to 0 for success:

#!/usr/local/bin/perl
 
use warnings;
use strict;
 
my $someText = "Learning Perl Language";
 
if((my $pos = index($someText, "Perl")) >= 0) {
  print "The text was located at the position $pos\n";
}
# it prints: The text was located at the position 9
Or you can replace the line of code that contains the if statement with:
 
if((my $pos = index($someText, "Perl")) > -1) {
and the result produced by running the code will be the same.

If you want to find out if a substring matches a bigger string and you don’t care about the substring position within the string, you can use a single line of code, like in the following example:

print "Found it!\n" if(index($someText,"Perl")>-1);

You can use the Perl index function to find the first occurrence of a substring in a string because this function searches for a substring from left to right, beginning with the first character of the string (if the position from where to begin the search is not specified).

The following simple example shows you a way to do it:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
my $str  = 'What you see is what you get';
my $pos;
 
print "Found it at $pos position!\n"
     if(($pos = index(lc $str,'you')) > -1);
# it prints: Found it at 5 position!
To make the search case insensitive the lc function is used. The index function returns the position of the first occurrence of the 'you' substring in the $str string.

If an occurrence is find, the script will print the position of the substring in the string, otherwise it will do nothing.

The following snippet code shows you how to find the positions of all the occurrences of a substring within a string, by using the index function:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize the original string
my $input = "Perl index, Perl chop, Perl hex";
my $pos = 0;
 
my @positions = ();
 
while (1) {
  $pos = index(lc $input, "perl", $pos);
  last if($pos < 0);
  push(@positions, $pos++);
}  
 
if (scalar (@positions) > 0) {
  print "substring found at positions: ", "@positions\n";
} else {
  print "substring not found\n";
}
Now I give a short description of this code. We initialize the scalar string $input and we intend to search for the substring "Perl" within this string. We use the index function to do this and a while loop to iterate through the $input string characters.

If there will be a match of the substring in the scalar $input string, the index function will return the position of that occurrence in the scalar variable $pos, which next is added to @positions array by using push function.

The loop will end when the Perl index function will return a value less than zero – no occurrence was found. In order lo leave the while loop, we use the loop control keyword last.

Finally, we print the @positions array which will display the position of all the occurrences of the substring "Perl" in the $input string. Please note the using of Perl lc (lowercase) function in order to make our comparisons case insensitive.

Running this snippet code will produce the following result:


    substring found at positions: 0 12 23

The following snippet code shows you how to count all the matches of a substring within a string, by using the index function:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize the original string
my $str = "one or two or three or four";
my ($pos, $count) = (0, 0);
 
while (1) {
  $pos = index(lc $str, "or", $pos);
  last if($pos++ < 0);
  $count++;
}  
 
print "Found $count matches\n";
We initialize the scalar string $str and we intend to count the matches of the substring "or" within this string.

We use the index function to do this and a while loop to iterate through the $str string characters. To make the search case insensitive, the lc function was used.

If there will be a match of the substring in the scalar $str string, the index function will return the position of that occurrence in the scalar variable $pos and the $count variable will be incremented.

The loop will end when the Perl index function will return a value less than zero – no occurrence was found.

In order to leave the while loop, we use the last loop control keyword and the if statement to check if the value of the $pos variable is less than 0 (please note the incrementing of the $pos variable after checking its value).

If you’ll run this code, you’ll get as result the following message:

Found 3 matches
 
Another approach is to use the regular expressions, as you can see in the following example:
 
#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize the original string
 
my $str = "one or two or three or four";
my $count = 0;
 
$count++ while $str =~ /or/ig;
 
print "Found $count matches\n";
In this example a while loop was wrapped around a pattern match.

The /i modifier let you make the search case insensitive and /g global modifier makes to search all the matches of the substring.

If a match is found, the $count variable is incremented.

The output is the same as in the previous example.

In the searching process using the Perl index function, you can remove the leading and trailing spaces from the string where you search.

Perl has not a build in trim function (like PHP) but you can simulate it by creating a subroutine trim with the help of the regular expressions.

See the next code snippet:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
my $input = '    Perl functions: trim, index';
my $substr = 'functions';
print "position without trim: ", index($input,$substr), "\n";
print "position with trim: ", index(trim($input),$substr), "\n";
 
# Declare the trim subroutine
sub trim
{
  my $string = shift;
  $string =~ s/^\s+//;           
  $string =~ s/\s+$//;
  return $string;        
}
In this example we print first the position of the $substr in the $input string and then the position of the same substring after removing whitespaces from the $input string.

You get the output: 

    position without trim: 9
    position with trim: 5

(the $input string value has 4 spaces before Perl word – you can’t count them on the screen!).