⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Strings and Sorting (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" />

<meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" />

</head><body bgcolor="#ffffff">

<img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map>

<div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch15_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>




<h1 class="chapter">Chapter 15. Strings and Sorting</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
  <p> <a href="#lperl3-CHP-15-SECT-1">Finding a Substring with index</a><br />
<a href="ch15_02.htm">Manipulating a Substring with substr</a><br />
<a href="ch15_03.htm">Formatting Data with sprintf</a><br />
<a href="ch15_04.htm">Advanced Sorting</a><br />
<a href="ch15_05.htm">Exercises</a><br /></p></div>

<p><a name="INDEX-997" /></a>As we
mentioned near the beginning of this book, Perl is designed to be
good at solving programming problems that are about 90% working with
text and 10% everything else. So it's no surprise that Perl has
strong text processing abilities, including all that we've done
with regular expressions. But sometimes the regular expression engine
is too fancy, and you'll need a simpler way of working with a
string, as we'll see in this chapter.
</p>

<div class="sect1"><a name="lperl3-CHP-15-SECT-1" /></a>
<h2 class="sect1">15.1. Finding a Substring with index</h2>

<p><a name="INDEX-998" /></a>Finding
a substring depends on where you have lost it. If you happen to have
lost it within a bigger string, you're in luck, because the
<tt class="literal">index</tt><a name="INDEX-999" /></a>
<a name="INDEX-1000" /></a> function can help you out.
Here's how it looks:
</p>

<blockquote><pre class="code">$where = index($big, $small);</pre></blockquote>

<p>Perl locates the first occurrence of the small string within the big
string, returning an integer location of the first character. The
character position returned is a zero-based value -- if the
substring is found at the very beginning of the string,
<tt class="literal">index</tt> returns <tt class="literal">0</tt>. If
it's one character later, the return value is
<tt class="literal">1</tt>, and so on. If the substring can't be
found at all, the return value is <tt class="literal">-1</tt> to indicate
that.<a href="#FOOTNOTE-332">[332]</a> In this example, <tt class="literal">$where</tt> gets
<tt class="literal">6</tt>:
</p><blockquote class="footnote"> <a name="FOOTNOTE-332" /></a><p>[332]Former C programmers will recognize this as
being like C's index function. Current C programmers ought to
recognize it as well -- but by this point in the book, you should
really be a <em class="emphasis">former</em>  C programmer.</p>
</blockquote>

<blockquote><pre class="code">my $stuff = "Howdy world!";
my $where = index($stuff, "wor");</pre></blockquote>

<p>Another way you could think of the position number is the number of
characters to skip over before getting to the substring. Since
<tt class="literal">$where</tt> is <tt class="literal">6</tt>, we know that we
have to skip over the first six characters of
<tt class="literal">$stuff</tt> before we find <tt class="literal">wor</tt>.
</p>

<p>The <tt class="literal">index</tt> function will always report the
location of the <em class="emphasis">first
found</em><a name="INDEX-1001" /></a> occurrence of the
substring. But you can tell it to start searching at a later point
than the start of the string by using the optional third parameter,
which tells <tt class="literal">index</tt> to start at that position:
</p>

<blockquote><pre class="code">my $stuff  = "Howdy world!";
my $where1 = index($stuff, "w");               # $where1 gets 2
my $where2 = index($stuff, "w", $where1 + 1);  # $where2 gets 6
my $where3 = index($stuff, "w", $where2 + 1);  # $where3 gets -1 (not found)</pre></blockquote>

<p>(Of course, you wouldn't normally search repeatedly for a
substring without using a loop.) That third parameter is effectively
giving a minimum value for the return value; if the substring
can't be found at that position or later, the return value will
be <tt class="literal">-1</tt>.
</p>

<p>Once in a while, you might prefer to have the <em class="emphasis">last
found</em><a name="INDEX-1002" /></a> occurrence of
the substring.<a href="#FOOTNOTE-333">[333]</a> You can get that with the
<tt class="literal">rindex</tt><a name="INDEX-1003" /></a> function. In this example, we can find
the last slash, which turns out to be at position
<tt class="literal">4</tt> in a string:
</p><blockquote class="footnote"> <a name="FOOTNOTE-333" /></a><p>[333]Well, it's not really the last
one found -- Perl cleverly starts searching from the other end of
the string, and then returns the first location it finds, which
amounts to the same result. Of course, the return value is the same
zero-based number as we always use for describing locations of
substrings.</p> </blockquote>

<blockquote><pre class="code">my $last_slash = rindex("/etc/passwd", "/");  # value is 4</pre></blockquote>

<p>The <tt class="literal">rindex</tt> function also has an optional third
parameter, but in this case it effectively gives the
<em class="emphasis">maximum</em> permitted return value:
</p>

<blockquote><pre class="code">my $fred = "Yabba dabba doo!";
my $where1 = rindex($fred, "abba");  # $where1 gets 7
my $where2 = rindex($fred, "abba", $where1 - 1);  # $where2 gets 1
my $where3 = rindex($fred, "abba", $where2 - 1);  # $where3 gets -1</pre></blockquote>

</div>










<hr width="684" align="left" />
<div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch15_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">14.8. Exercises</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">15.2. Manipulating a Substring with substr</td></tr></table></div>
<hr width="684" align="left" />

<img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" />
<p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p>

<map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map>

</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -