Softpanorama

Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
May the source be with you, but remember the KISS principle ;-)
Skepticism and critical thinking is not panacea, but can help to understand the world better

Perl index and rindex built-in functions

News Perl Language Recommended Links Perl string operations Reference Shell tr command
sort substr split sprintf  index and rindex chomp
join Perl uc, lc, ucfirst and lcfirst functions x operator in Perl Regular expressions    
Nikolai Bezroukov. Simplified Perl for Unix System Administrators Trim Pipes in Perl Perl history Humor Etc

Introduction

The index() function is used to determine the position of a letter or a substring in a string.   Depending on what you are trying to achieve, the index() function may be faster or more easy to understand than using a regular expression (especially on string of one or several megabytes. It also may be simpler for splitting the string into just two parts then split function.

In perl index function has two forms index and rindex. The letter search from the end of the string. both return index of the first letter of match or -1 if no match available.

Let's quote Perl man page for a more precise definition:

index STR,SUBSTR,POSITION  -- Extended form !!! many people forget or just do not know about this option If POSITION is specified, returns the last occurrence at or before that position.
 
index STR,SUBSTR  -- classic PL/1 form
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. The return value is based at 0  (or whatever you've set the $[  variable to--but don't do that). If the substring is not found, returns one less than the base, ordinarily -1.

The index function search its first operand (string) in the second operand (substring) and return the offset of the first substring found. The rindex function returns the offset of the last substring found. 

Let's assume the variable $string contains the value abracadabra. Here are some examples: 

$string="abracadabra";
print index ($string, "ab")."\n";     # will print 0 (the 1st letter has index 0)
print index ($string, "abc")."\n";    # will print -1
print index ($string, "ab", 2)."\n";  # will print 7 (staring pos is 2)
print index ($string, "ra", 3)."\n";  # will print 9
print rindex ($string, "ab");         # will print 7 (last "ab" in the string)
print index($string, 'rb', 3);        # will print -1 as there is no such string from the third position

Perl index function  returns -1 if the string is not found, which looks logical as it is the index of the last character of the string and that's where unsuccessful matching stops. But this is not true if the third augment is used in the loop.  Here things became tricky. Somebody decided to optimize the behaviour of this function in loops. As the result you enter the minefield. In this case index do not return -1. It just does not change the value of the variable, if function fails to find  the string.  In other words, in some circumstances,  index behaves like regular expression, which does not change the value of $1, $2, etc, if match failed. I observed this behaviour  in Perl v5.10 (RHEL 6, CentOs 6) and Perl 5.22.

index always return offset counted from the beginning of the string even if the third argument is present if it have found the string in question.

If the string is not found the result is -1 (zero corresponds to the first letter of the string)

As one can see index function is not greedy -- it finds the first substring in the string that matches the argument and stops at this point. 

 Therefore if you need to find position of a given substring in the larger string  it can be used instead of using regular expressions.

If the  source string is very long you can use some optimizations.  See Knuth–Morris–Pratt algorithm - Wikipedia (with code avaible at Knuth-Morris-Pratt Algorithm

In general the more you know about the search string and the text in which you search, the faster you can search. If some/most of the symbols in text you search do not occur in the search string and you are simply interested in if  (or how many times) the search string occurs in the target string all those "missing" symbols can be translated to a single "non-occurring" symbol and string them can be compressed with  tr by removing all consecutive "non-occurring symbols".  For example

$text='We search for word abba in this string';
$str='abba';
$text=~tr/abba/?/cs;
print "text='$text'\n";
As you see from the result of execution of this fragment in this case we would compress the search string to
text='?a?abba?'

Searching for string that contains double quotes

If you have a string that contain double quotes and want to interpolate variable in this string instead of double quotes it's more convenient to use function qq like in

$a=qq(<font face="$font" color="$color">);

This is the best way to avoid errors connected with forgetting to escape all double quotes in such strings. Compare example above with much less intuitive variant using escape symbol:

$a="&pce=\"$font\" color=\"$color\">"

Also please  remember that a double backslash in double quoted literals represents just one backslash.

$path="C:\\SFU\\bin";
if ( index($path,"\\SFU\\") >-1 ) {
   print "The directory belongs to Microsoft Services for Unix\n";
}

Often one needs to extract the file name at the end of the path. You might do this by searching for the last backslash using the rindex function and then using substr to return the sub-string. For example:

$fullname = qq(C:/WINDOWS/TEMP/SOME.DAT);
$d=index($fullname,':'); #
$drive=substr($fullname, 0, $d);
$p = rindex($fullname, '/') + 1; # index of the first letter after
	
$fname = substr($fullname,$p); # note that we use 2 arguments

print("File $fileName is on the drive $d\n");

Note that in the example above we used a special form of substr invocation -- if the third parameter(the length) is not supplied to substr, it simply returns the sub-string that starts at the position specified by the second parameter until the end of the string. By omitting the third argument we can avoid errors when we miscalculate the length of the substring

Here is relevant part of manpage:

 

The ability to specify starting position

The important innovation of Perl implementation of index function in comparison with PL/1 and REXX is that you can specify the starting position of the search.

Like in substr in case it is negative it will be counted from the end of the string.

Another important difference is that in case the string is not found index will return -1 not 0. This is pretty logical design decision as it is corresponds to the index of the last element of the string.

Extracting part of the string before and after match

If you have found some string then the most typical next step is extracting part of the string before or after the match. using index and substr if often more flexible option the non-greedy regex matching, especially due to case that sometimes non greedy matching in perl works not as you expect:

#
# processomg h4 header]
#
   $h4=substr($news_item,0,$h4_end); #full h4 header
   $h4_bracket=index($h4,'>'); # end bracket of h4
   $h4=substr($h4,$h4_bracket+1);
   ($debug) && logme("h4=^$h4^\n");
   $chunk_new_fname=parse_h4($h4); #produces timestamp for the file name and correct file name extracted from the title
   return if (length($chunk_new_fname)<10);
   if (index($h4,'|')>-1) {
      # false TOC entries for <h6>  [Apr 05, 2014] 
      logme(" h4 is structured like h6 whith '|' delimiter\n");
      substr($chunk_fname,0,1)='h';
      `mv $chunk_fqn $chunk_dir/$chunk_fname`;
      return;
   }
   $chunk_body=substr($news_item,$h4_end+5);  # body of the news item with H4 block stripped
#
# process h5 if it is exists. 
# the problem here is that there can be multiple <h5> tags
   
   $h5='';
   $h5_start=index(substr($chunk_body,0,80),'<h5'); # can be comment after <h4, althouth this not normal
   while($h5_start > -1) {
      $h5_end=index($chunk_body,'</h5>',$h5_start);
      $h5.='<br>.<br>'.substr($chunk_body,$h5_start+4,$h5_end-$h5_start-4);
      $chunk_body=substr($chunk_body,$h5_end+5);  # body of the news item with H% stripped
      $h5_start=index(substr($chunk_body,0,80),'<h5');
   }    
#
# process <h6 with bibliodata
#
   # NOTE: -s --treat string a single lime . matches "\n". 
   $bk_start=index(substr($chunk_body,0,80),'<blockquote>');
   if ($bk_start==-1) {
      # no opening blockquote  [Feb 27, 2015] 
      logme("???????????????????????????????????????????????? No openning blockquote found\n");
      substr($chunk_fname,0,1)='x';
      `mv $chunk_fqn $chunk_dir/$chunk_fname`;
      return;
   } 
   $chunk_body=substr($chunk_body,$bk_start+length('<blockquote>'));

The usage of rindex function

Classic use of rindex function is splitting fully qualified name into directory part and file name part.

$last_slash=rindex($chunk_fqn,'/');
$chunk_dir=substr($chunk_fqn,0, $last_slash);
$chunk_fname=substr($chunk_fqn,$last_slash+1);

You can also search closing tags from the end of the document. This way you can imitate nesting

$tags_start=rindex($news_item,'<!--TAGS:'); # tag list should be comment 
if ($tags_start==-1) { 
# false TOC entries for <h6> [Apr 05, 2014]  
logme("No Tags found in the news item. News chunk was just properly renamed\n");  
return; 
} 
 
$tags_end=index($news_item,"-->",$tags_start+9); 
$tag_list=substr($news_item,$tags_start+9,$tags_end-$tags_start-9); 
@tags=split(/\s+/,$tag_list); 

Achieving case insensitive matching

You can achieve case insensitive matching by using lc function for both SRT and SUBSTR arguments.

 

Limiting search to a part of the string

to limit serach to a part of the string use substr function

$h5_start=index(substr($chunk_body,0,80),'

Tips

when trying to get a part of the  string that are limited by two strings it is easy to commit "plus one" error extracting  one symbol more or one symbol less. Always check your code on the case when the second string directly follow next. for example

$between=substr($a,($k=index($a,'prefix')+length('prefix')),index($a,'suffix')-$k);

Here we assume that both search substrings are present in the string. If this is unknown, then the code will be substantially more complex but the key idea is the same (we also provide testing routine as an example how generally such things are done):

use strict;
my $prefix='start';
my $suffix='end';
my ($a,$delta,$result);
my $errors=0;
#
# Simple sest of extraction substring between two given strings
#
   for( my $i=0;$i<3;$i++ ){
       $delta='?' x $i;
       print "Test $i: Expected string returned should be '$delta'";
       $a="$prefix$delta$suffix";
       $result=mylib::between($a,$prefix,$suffix);
       if( $result ne $delta ){
          print "\nTest $i failed: after extracting substrenf between '$prefix' and '$suffix' in  '$a' the result is '$result' instead of '$delta'\n";
          $errors++;
       } else {
          print "\t[OK]\n";
       }
   }
   if( $errors==0 ){
      print "Test was successful\n";
   } else {
      print "ATTENTION: Some tests failed, see above\n";
   }
package mylib;
#
# Extracts substring between two given strings. Does not share any variables with main namespace
#
sub between
{
my ($source,$from,$to)=@_;
my ($k,$m,$len);
   $k=index($source,$from);
   if ($k>-1) {
      $k+=length($from);
      $m=index($source,$to);
      if ($m>-1 && $m>$k) {
         $len=$m-$k;
         return substr($source,$k,$len); # sucess
      }
   }
   return ''; # unsuccessful execution returns zero length string
}

Please note that even such a simple test requires program that is longer then the subroutine we are testing ;-). After execution you will see something like:

[0] # perl between_test.pl
Test 0: expected string returned should be ''   [OK]
Test 1: expected string returned should be '?'  [OK]
Test 2: expected string returned should be '??' [OK]
Test was successful

Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

Using the Perl index() function

regex - Regular expression in index function - Stack Overflow

Q:

I am looking for occurrence of "CCGTCAATTC(A|C)TTT(A|G)AGT" in a text file.

$text = 'CCGTCAATTC(A|C)TTT(A|G)AGT'; if ($line=~/$text/){ chomp($line); $pos=index($line,$text); }

Searching is working, but I am not able to get the position of "text" in line. It seems index does not accepts a regular expression as substring.

How can I make this work. Thanks

A:

The @- array holds the offsets of the starting positions of the last successful match. The first element is the offset of the whole matching pattern, and subsequent elements are offsets of parenthesized subpatterns. So, if you know there was a match, you can get its offset as $-[0].

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites

Top articles

Sites

...

index - perldoc.perl.org

rindex Function - Perl String Tutorials



Etc

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2018 by Dr. Nikolai Bezroukov. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March, 12, 2019