Perl | index() Function Last Updated : 06 Mar, 2023 Comments Improve Suggest changes 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 | index() Function C Code_Mech Follow Improve Article Tags : Perl Perl-String Perl-function Perl-String-Functions Similar Reads Perl | int() function int() function in Perl returns the integer part of given value. It returns $_ if no value provided. Note that $_ is default input which is 0 in this case. The int() function does not do rounding. For rounding up a value to an integer, sprintf is used. Syntax: int(VAR) Parameters: VAR: value which is 1 min read 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 | List Functions A list in Perl is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol whereas list varia 4 min read Perl | oct() Function oct() function in Perl converts the octal value passed to its respective decimal value. For example, oct('1015') will return '525'. This function returns the resultant decimal value in the form of a string which can be used as a number because Perl automatically converts a string to a number in nume 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 | chr() Function The chr() function in Perl returns a string representing a character whose Unicode code point is an integer. Syntax: chr(Num)Parameters: Num : It is an unicode integer value whose corresponding character is returned .Returns: a string representing a character whose Unicode code point is an integer. 1 min read Perl | cos() Function This function is used to calculate cosine of a VALUE or $_ if VALUE is omitted. The VALUE should be expressed in radians. Syntax: cos(VALUE) Parameters: VALUE in the form of radians Returns: Function returns cosine of VALUE. Example 1: Perl #!/usr/bin/perl # Calling cos function $var = cos(5); # Pri 1 min read Like