Good Place. Good Things. Good Time.

Perl index

Perl index

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: $myresult1n";
print "two: $myresult2n";
print "three: $myresult3n";
print "four: $myresult4n";
print "five: $myresult5n";

Output:

Perl index output 1

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: $myresult1n";
print "two: $myresult2n";
print "three: $myresult3n";
print "four: $myresult4n";
print "five: $myresult5n";

Output:

Perl index output 2

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 –

  1. Perl Hash
  2. Perl sort()
  3. Perl last
  4. Split in Perl

The post Perl index appeared first on EDUCBA.

Read original article here: Perl index

Read original article here: Perl index



Disclaimers and Denial of responsibility..!

Denial of responsibility! TheTopMag.com is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – jamesonline999@gmail.com . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.