Perl index() function

Perl index() function

The index() function is used to identify or search for the  position of a letter or a substring in a string. It will return the position value of the first occurance of the substring within the string.

$index = index(‘iguddy’, ‘gud’);

print $index . “n”;

This will print 1, which is the index value of the start of the substring gud

The index() function is similar to a regular expression call, but without all the overhead of the regex search patterns. It simply returns the index value of the substring if it exists within the string.

Example 1:

Let’s consider you are searching for a particular letter ‘u’ in the following string “iguddy.com”. The following code snippet will help you find the letter ‘u’ in the String “IGuddy.com”

 #!/usr/bin/perl

my $string = ‘iguddy.com’;

my $letter = ‘u’;

my $result = index($string, $letter);

print “Result: $resultn”;

This program gives you:

  Result: 2

Example 2:

The index() function will return -1 if the substring is not present in the actual string.For example, we can look for the letter “A” in the string “iguddy.com”:

 #!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘A’;

my $result = index($string, $char);

print “Result: $resultn”;

The program outputs:

  Result: -1

Example 3:

If the letter we’re searching for appears more than once in the string, index() return the index of the first occurrence of the letter.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $result = index($string, $char);

print “Result: $resultn”;

This program gives you:

Result: 3

Example 4 :

Instead of searching for a character we can also search for a substring using index() function.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $substr = ‘gud’;

my $result = index($string, $substr);

print “Result: $resultn”;

This program gives you:

Result: 1

Example 5:

The index() function always return the index of first occurrence of the letter. We can also look for the second occurrence as below,

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $offset = 2;

my $result = index($string, $char, $offset);

print “Result: $resultn”;

The program outputs:

Result: 4

Example 6:

Index() in a loop to find out all the occurrence of the substring.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $offset = 0;

my $result = index($string, $char, $offset);

while ($result != -1) {

print “Found $char at $resultn”;

$offset = $result + 1;

$result = index($string, $char, $offset);

}

The above will gives the following output

Found e at 3

Found e at 4

Example 7:

To find the last occurrence of the substring using index() frunction.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $result = rindex($string, $char);

print “Result: $resultn”;

This would produce:

Result: 4

Example 8:

Is there any way of ignoring case in Perl’s index function?  The Answer is “Yes” there is a way. Just find below the code snippet,

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $substr = ‘GUD’;

if (index lc($string),lc($substr) > -1 {

print “Found : Result: $resultn”;

}else{

print “Not Found : Result: $resultn”;

}

This program gives you:

Result: 1

Leave a comment