Perl | index() Function Last Updated : 06 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report This function returns the position of the first occurrence of given substring (or pattern) in a string (or text). We can specify start position. By default, it searches from the beginning(i.e. from index zero). Syntax: # Searches pat in text from given index index(text, pat, index) # Searches pat in text index(text, pat) Parameters: text: String in which substring is to be searched.pat: Substring to be searched.index: Starting index(set by the user or it takes zero by default). Returns: -1 on failure otherwise Position of the matching string. Example 1: Perl #!/usr/bin/perl # String from which Substring # is to be searched $string = "Geeks are the best"; # Using index() to search for substring $index = index ($string, 'the'); # Printing the position of the substring print "Position of 'the' in the string: $index\n"; Output:Position of 'the' in the string: 10 Example 2: Perl #!/usr/bin/perl # String from which Substring # is to be searched $string = "Geeks are the best"; # Defining the starting Index $pos = 3; # Using index() to search for substring $index = index ($string, 'Geeks', $pos); # Printing the position of the substring print "Position of 'Geeks' in the string: $index\n"; Output:Position of 'Geeks' in the string: -1 Here, in the second example the position is set to '3', i.e. the starting index from where searching is to begin is from 3rd position. Hence, the substring is not found in the String. Example - 3: Here we will see what will happen if the string/character we are trying to find is present more than once in the real string. Perl #!/usr/bin/perl $string = 'Geeks for Geeks'; $char = 'e'; $res = index($string, $char); print("Position of $char is : $res\n"); Output - Now as we can see it returned the output as 1 which is the first occurrence of 'e'. If we have the required character present more than once in our string, index will return the first occurrence by default. Example - 4 : Here we will see how we can get all the occurrences of a character in the original string. Perl #!/usr/bin/perl my $string = 'Geeks for Geeks'; my $char = 'e'; my $start = 0; my $res = index($string, $char,$start); # Checking till the char is not found while ($res != -1 ) { print("Occurence of $char at $res\n"); # incrementing $start value each iteration # so that it doesn't print same index twice $start=$res+1; # Finding the index using the updated # start value $res = index($string,$char,$start); } Output - Comment More infoAdvertise with us Next Article Perl | abs() function C Code_Mech Follow Improve Article Tags : Perl Perl-String Perl-function Perl-String-Functions Similar Reads Perl | ord() Function The ord() function is an inbuilt function in Perl that returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string. Syntax: ord string Parameter: This function accepts a single par 1 min read Perl | grep() Function The grep() function in Perl used to extract any element from the given array which evaluates the true value for the given regular expression. Syntax: grep(Expression, @Array) Parameters: Expression : It is the regular expression which is used to run on each elements of the given array.@Array : It is 2 min read Perl | keys() Function keys() function in Perl returns all the keys of the HASH as a list. Order of elements in the List need not to be same always, but, it matches to the order returned by values and each function. Syntax: keys(HASH) Parameter: HASH: Hash whose keys are to be printed Return: For scalar context, it return 1 min read Perl | rindex() Function rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given 2 min read Perl | abs() function This function returns the absolute value of its argument. If a pure integer value is passed then it will return it as it is, but if a string is passed then it will return zero. If VALUE is omitted then it uses $_ Syntax: abs(VALUE) Parameter: VALUE: It is a required number which can be either positi 2 min read Perl | each() Function This function returns a Two-element list consisting of the key and value pair for the next element of a hash when called in List context, so that you can iterate over it. Whereas it returns only the key for the next element of the hash when called in scalar context. Syntax: each MY_HASH Parameter: M 1 min read Like