|
Home | Switchboard | Unix Administration | Red Hat | TCP/IP Networks | Neoliberalism | Toxic Managers |
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and bastardization of classic Unix |
|
|
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?'
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:
- q/STRING/
- qq/STRING/
- qx/STRING/
- These are not really functions, but simply syntactic sugar to let you avoid putting too many backslashes into quoted strings.
- The q operator is a generalized single quote, and
- the qq operator a generalized double quote.
- The qx operator is a generalized backquote.
Any non-alphanumeric delimiter can be used in place of /, including newline.
If the delimiter is an opening bracket or parenthesis, the final delimiter will be the corresponding closing bracket or parenthesis. (Embedded occurrences of the closing bracket need to be backslashed as usual.) Examples:
$foo = q!I said, "You said, 'She said it.'"!; $bar = q('This is it.'); $today = qx{ date }; $_ .= qq *** The previous line contains the naughty word "$&".\n if /(ibm|apple|awk)/; # :-)
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.
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>'));
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);
You can achieve case insensitive matching by using lc function for both SRT and SUBSTR arguments.
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
|
Switchboard | ||||
Latest | |||||
Past week | |||||
Past month |
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]
.
Google matched content |
...
rindex Function - Perl String Tutorials
Society
Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers : Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism : The Iron Law of Oligarchy : Libertarian Philosophy
Quotes
War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Somerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Bernard Shaw : Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds : Larry Wall : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOS : Programming Languages History : PL/1 : Simula 67 : C : History of GCC development : Scripting Languages : Perl history : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history
Classic books:
The Peter Principle : Parkinson Law : 1984 : The Mythical Man-Month : How to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor
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-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) 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 to buy a cup of coffee for authors of this site |
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 Softpanorama society. 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