Function index() in Perl

In the Perl programming language has a built-in function index(). You can use this function when working with strings. Using this function, you can test whether a character or substring part of the string, and also to know the position on which the character or substring in the string.

Here's an example:

▶ Run
#!/usr/bin/perl

print( index('Hello', 'o') );

This program will display the number 4. Letter 'o' is the fifth character in the string 'Hello', In the programming language Perl, the first character in the string has number 0. Therefore, for the fifth character function index() returns the number 4

The fact that the first character in the string has number 0 absolutely the same way as numbered the elements in a Perl array, the first element in the array has room 0 (but in the case of an array there is one subtlety).

That's what will take the same program if you use different values for the second argument:

  • 'H'0
  • 'e'1
  • 'l'2 — in line 'Hello' there are two letters 'l'if the function index() passes two arguments, it always returns the number for the first occurrence of a character in a string
  • 'o'4
  • 'z'-1 — letter 'z' in the line no, in this case, the function returns the value -1
  • 'h'-1 — function index() case-sensitive. In the string is the letter 'H' in the upper register, but there is no letter 'h' in lower case, so for 'h' the function returns the special value -1 — not found
  • 'Hello'0 — line 'Hello' is included in line 'Hello' starting with the first symbol, the first symbol has the number 0that returns a function
  • 'el'1 — string 'el' included in the row 'Hello' starting with the second symbol
  • 'asdf'-1 — this line is not part of the string 'Hello'

Arguments

my $index1 = index($str, $substr);
my $index2 = index($str, $substr, $position);

Function index() must specify either two or three arguments. If you do not specify any argument, or specify only one, it will error:

Not enough arguments for index at script.pl line 3, near "index() "
Execution of script.pl aborted due to compilation errors.

If you specify more than 3 arguments, it will error:

Too many arguments for index at script.pl line 3, near "'a') "
Execution of script.pl aborted due to compilation errors.

The first and the second argument to the Perl interprets as strings. If there is a third argument, it is perceived as a number.

Function index() looks for the string contained in the second argument in the row that contains in the first argument starting at the position specified in the third argument (or the beginning of the string if the third the argument is not specified).

Return value

The result of the function index() is always an integer.

  • If the number is positive or 0, this means that the string from the second argument is the substring of the first argument. The value means that the position of the first line which began with a coincidence. The search occurs from the beginning of the string if the third argument is not specified, or from the position specified in the third argument.
  • The number -1 means that the substring in the string is missing

If you do not use the return value in Perl script is use warnings;, then the screen displays a warning:

Useless use of index in void context at script.pl line 6.

For example, such a warning will be given the following program:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

index('Hello', 'o');

An example of using index() with three arguments

Here is a sample program. Then use the function index() to find the substring 'Ab' in the line in which this the string substring appears twice. If the functions index() not to pass the third argument, it is the same as if you pass 0 as the third argument.

▶ Run
#!/usr/bin/perl

print( index('00 Ab Ab', 'Ab', 0) );

The program will display the number 3 means that the substring 'Ab' is in the string starting at the fourth character.

But the program that displays the result of the function index() for different values of the third argument:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use feature qw(say);

foreach my $position (0..9) {
    say( $position, ' ', index('00 Ab Ab', 'Ab', $position) );
}

This program will print:

0 3
1 3
2 3
3 3
4 6
5 6
6 6
7 -1
8 -1
9 -1

index() searches for a substring in the string starting at the position specified in the third argument. Substring 'Ab' found in the string twice. For the first time since the index 3, the second time from index 6.

'00 Ab Ab'
 01234567

Official documentation

Here is the output of the command perldoc -f index:

       index STR,SUBSTR,POSITION
       index STR,SUBSTR
               The index function searches for one string within another, but
               without the wildcard-like behavior of a full regular-expression
               pattern match.  It returns the position of the first occurrence
               of SUBSTR in STR at or after POSITION.  If POSITION is omitted,
               starts searching from the beginning of the string.  POSITION
               before the beginning of the string or after its end is treated
               as if it were the beginning or the end, respectively.  POSITION
               and the return value are based at zero.  If the substring is
               not found, "index" returns -1.

Related topics

Other articles

Comments