ch17_02.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 98 行

HTM
98
字号
<html><head><title>Picking Items from a List with grep (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="ch17_01.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="ch17_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">17.2. Picking Items from a List with grep</h2><p>Sometimes you'll want only certain items from a list. Maybeit's only the odd numbers selected from a list of numbers, ormaybe it's only the lines mentioning <tt class="literal">Fred</tt>from a file of text. As we'll see in this section, picking someitems from a list can be done simply with the<a name="INDEX-1097" /><tt class="literal">grep</tt>operator.</p><p>Let's try that first one and get the odd numbers from a largelist of numbers. We don't need anything new to do that:</p><blockquote><pre class="code">my @odd_numbers;foreach (1..1000) {  push @odd_numbers, $_ if $_ % 2;}</pre></blockquote><p>That code uses the <a name="INDEX-1098" /> <a name="INDEX-1099" />modulus operator(<tt class="literal">%</tt>), which we saw in <a href="ch02_01.htm">Chapter 2, "Scalar Data"</a>.If a number is even, that number "mod two" gives zero,which is false. But an odd number will give one; since that'strue, only the odd numbers will be pushed onto the array.</p><p>Now, there's nothing wrong with that code as itstands -- except that it's a little longer to write andslower to run than it might be, since Perl provides the<tt class="literal">grep</tt> operator:</p><blockquote><pre class="code">my @odd_numbers = grep { $_ % 2 } 1..1000;</pre></blockquote><p>That line gets a list of 500 odd numbers in one quick line of code.How does it work? The first argument to <tt class="literal">grep</tt> is ablock that uses <tt class="literal">$_</tt> as a placeholder for each itemin the list, and returns a Boolean (true/false) value. The remainingarguments are the list of items to search through. The<tt class="literal">grep</tt> operator will evaluate the expression oncefor each item in the list, much as our original<tt class="literal">foreach</tt> loop did. For the ones where the lastexpression of the block returns a true value, that element isincluded in the list that results from <tt class="literal">grep</tt>.</p><p>While the <tt class="literal">grep</tt> is running, <tt class="literal">$_</tt>is aliased to one element of the list after another. We've seenthis behavior before, in the <tt class="literal">foreach</tt> loop.It's generally a bad idea to modify <tt class="literal">$_</tt>inside the <tt class="literal">grep</tt> expression, because this willdamage the original data.</p><p>The <tt class="literal">grep</tt><a name="INDEX-1100" /> operator sharesits name with a classic Unix utility that picks matching lines from afile by using regular expressions. We can do that with Perl's<tt class="literal">grep</tt>, which is much more powerful. Here we pullonly the lines mentioning <tt class="literal">fred</tt> from a file:</p><blockquote><pre class="code">my @matching_lines = grep { /\bfred\b/i } &lt;FILE&gt;;</pre></blockquote><p>There's a simpler syntax for <tt class="literal">grep</tt>, too. Ifall you need for the selector is a simple expression (rather than awhole block), you can just use that expression, followed by a comma,in place of the block. Here's the simpler way to write thatlatest example:</p><blockquote><pre class="code">my @matching_lines = grep /\bfred\b/i, &lt;FILE&gt;;</pre></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch17_01.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="ch17_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">17. Some Advanced Perl Techniques</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">17.3. Transforming Items from a List with map</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 + =
减小字号Ctrl + -
显示快捷键?