appa_08.htm

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

HTM
119
字号
<html><head><title>Answers to Chapter 9 Exercises (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="appa_07.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="appa_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.8. Answers to Chapter 9 Exercises</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">/($what){3}/</pre></blockquote><p>Once <tt class="literal">$what</tt> has been interpolated, this gives apattern resembling <tt class="literal">/(fred|barney){3}/</tt>. Without theparentheses, the pattern would be something like<tt class="literal">/fred|barney{3}/</tt>, which is the same as<tt class="literal">/fred|barneyyy/</tt>. So, the parentheses are required.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">@ARGV = '/path/to/perlfunc.pod';  # or mentioned on the command linewhile (&lt;&gt;) {  if (/^=item\s+([a-z_]\w*)/i) {    print "$1\n";                 # print out that identifier name  }}</pre></blockquote><p>With what we've shown you so far, the only way to open anarbitrary file for input is to use the diamond operator (or to useinput redirection, perhaps). So we put the path to<tt class="literal">perlfunc.pod</tt> into <tt class="literal">@ARGV</tt>.</p><p>The heart of this program is the pattern, which looks for anidentifier name on an <tt class="literal">=item</tt> line. The exercisedescription was ambiguous, in that it didn't say whether<tt class="literal">=item</tt> had to be in lower case; the author of thispattern seems to have decided that it should be a case-insensitivepattern. If you interpreted it otherwise, you could have used thepattern <tt class="literal">/^=item\s+([a-zA-Z_]\w*)/</tt>.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">@ARGV = '/path/to/perlfunc.pod';  # or mentioned on the command linemy %seen;                         # (optionally) declaring the hashwhile (&lt;&gt;) {  if (/^=item\s+([a-z_]\w*)/i) {    $seen{$1} += 1;               # a tally for each item  }}foreach (sort keys %seen) {  if ($seen{$_} &gt; 2) {            # more than twice    print "$_ was seen $seen{$_} times.\n";  }}</pre></blockquote><p>This one starts out much like the previous one, but declares the hash<tt class="literal">%seen</tt> (in case <tt class="literal">use strict</tt> mightbe in effect). This is called <tt class="literal">%seen</tt> because ittells us which identifier names we've seen so far in theprogram, and how many times. This is a common use of a hash. Thefirst loop now counts each identifier name, as an entry in<tt class="literal">%seen</tt>, instead of printing it out.</p><p>The second loop goes through the keys of <tt class="literal">%seen</tt>,which are the different identifier names we've seen. It sortsthe list, which (although not specified in the exercise description)is a courtesy to the user, who might otherwise have to search for thedesired item in a long list.</p><p>Although it may not be obvious, this program is pretty close to areal-world problem that most of us are likely to see. Imagine thatyour webserver's 400-megabyte logfile has some information youneed. There's no way you're going to read that file onyour own; you'll want a program to match the information youneed (with a pattern) and then print it out in some nice reportformat. Perl is good for putting together quick programs to do thatsort of thing.</p></li></ol><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appa_07.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="appa_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.7. Answers to Chapter 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">A.9. Answer to Chapter 10 Exercise</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 + -
显示快捷键?