Updated April 4, 2023
Introduction to Perl index
The index is the function available in Perl to get the index. If we want to know the index or the position of the substring, then we can use this function to get the value. By the use of the index function, we can also get the position of the character in the string. In the short index, the function is used to get the position or index where the substring or character is placed in the given string; inside index methods, we just have to pass our parameters. In the coming section, we will discuss this method more in detail to better understand the method to use.
Syntax
We can directly use this function in the program without including any library because it is an inbuild function available in Perl to deal with String. Let’s see its syntax for better understating how to use it in programming see below;
1. index(text, substring, index): calculating with index.
In the above syntax, we are passing three parameters as the input. The last parameter is not mandatory to pass.
2. index(text, substring): without index being passed.
In the above syntax, we pass two parameters as input to calculate and return us the value.
index("String to find from", "string to search");
index("some string", "some",0)
Above, you can see the sample syntax for the index method to use in Perl programming.
How does index work in Perl?
As of now, we know that index is used to get the position of the element in the string. This method can take two or three parameters. We have some scenarios where we want to know the actual position of the character or any substring into the string so we can call this method and get the position. By the use of this method, we can even specify the index of the substring from which we want them to evaluate the position. In this section, we will first discuss the method signature of the index and return type. Let’s see the signature of the method in detail see below;
1. index(text, substring, index): This method takes three parameters as the input. By using this method, we can get the actual position of the character or substring inside a string message. Let’s discuss its signature in detail;
- text: This parameter is used to pass our actual string from which we want to calculate our character or substring position value. We can create a normal string in Perl and pass this inside the method.
- substring: This parameter is the original substring that we want the index() method to calculate its value and return the position for that element in a given string.
- index: This is the optional parameter in this method. If you want to set the starting index for the substring, then mention this parameter while calling the index() method in Perl. If we do not mention it, it just takes up the default value of ‘0’.
2. index(text, substring): This method is also used to get the index of the substring. But it has one difference from the above one is that here we are not mentioning the index or the starting index for a substring so that it will take up as default of ‘0’.
3. Return type: This method returns an integer value on the successful finding of the character or substring inside the string; otherwise, it will return -1 as the value on failure.
Apart from all this, all the things are similar in both the methods available in Perl. These are the in-build method of Perl, so we do not need to mention or import any library in our project before using it. Let’s see one sample example for beginners how to use this in programming see below;
Example:
# Your code here!
$mystr = "Hello to find !!";.
$myresult = index ($mystr, 'find');
In the above lines of code, we are trying to find a substring into the string. First, we have created one string object and assign it some value. After this, we call the ‘index’ method and pass our string inside it with the value to be searched as ‘find’ in the string. So it returns us an integer value with its actual position in the string.
Examples
Different examples are mentioned below:
Example #1
In this example, we are using the index method without index param to get the substring position in Perl.
Code:
# Your code here!
$mystr1 = "I am first string ";
$mystr2 = "I am second string ";
$mystr3 = "I am third string ";
$mystr4 = "I am fourth string ";
$mystr5 = "I am fifth string ";
$myresult1 = index ($mystr1, 'first');
$myresult2 = index ($mystr2, 'second');
$myresult3 = index ($mystr3, 'third');
$myresult4 = index ($mystr4, 'fourth');
$myresult5 = index ($mystr5, 'fifth');
print "Result for each string are :::\n";
print "One: $myresult1\n";
print "two: $myresult2\n";
print "three: $myresult3\n";
print "four: $myresult4\n";
print "five: $myresult5\n";
Output:
Example #2
In this example, we are using the index method with index param to get the substring position in Perl.
Code:
# Your code here!
$mystr1 = "I am first string ";
$mystr2 = "I am second string ";
$mystr3 = "I am third string ";
$mystr4 = "I am fourth string ";
$mystr5 = "I am fifth string ";
$myresult1 = index ($mystr1, 'first', 3);
$myresult2 = index ($mystr2, 'second', 2);
$myresult3 = index ($mystr3, 'third', 1);
$myresult4 = index ($mystr4, 'fourth'. 1);
$myresult5 = index ($mystr5, 'fifth', 0);
print "Result for each string are :::\n";
print "One: $myresult1\n";
print "two: $myresult2\n";
print "three: $myresult3\n";
print "four: $myresult4\n";
print "five: $myresult5\n";
Output:
Conclusion
We may have some scenarios where we need to have the actual position of the substring from the string so we can use the index() method in Perl. We can also evaluate the position of the character by using the same method in Perl. Also, this can help me know whether the character or substring is present in the string or not.
Recommended Articles
This is a guide to the Perl index. Here we discuss How does index work in Perl and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –