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

📄 appa_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Answers to Chapter 4 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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.3. Answers to Chapter 4 Exercises</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">sub total {  my $sum;  # private variable  foreach (@_) {    $sum += $_;  }  $sum;}</pre></blockquote><p>This subroutine uses <tt class="literal">$sum</tt> to keep a running total.At the start of the subroutine, <tt class="literal">$sum</tt> is<tt class="literal">undef</tt>, since it's a new variable. Then, the<tt class="literal">foreach</tt> loop steps through the parameter list(from <tt class="literal">@_</tt>), using <tt class="literal">$_</tt> as thecontrol variable. (Note: once again, there's no automaticconnection between <tt class="literal">@_</tt>, the parameter array, and<tt class="literal">$_</tt>, the default variable for the<tt class="literal">foreach</tt> loop.)</p><p>The first time through the <tt class="literal">foreach</tt> loop, the firstnumber (in <tt class="literal">$_</tt>) is added to<tt class="literal">$sum</tt>. Of course, <tt class="literal">$sum</tt> is<tt class="literal">undef</tt>, since nothing has been stored in there. Butsince we're using it as a number, which Perl sees because ofthe numeric operator <tt class="literal">+=</tt>, Perl acts as ifit's already initialized to <tt class="literal">0</tt>. Perl thusadds the first parameter to <tt class="literal">0</tt>, and puts the totalback into <tt class="literal">$sum</tt>.</p><p>Next time through the loop, the next parameter is added to<tt class="literal">$sum</tt>, which is no longer <tt class="literal">undef</tt>.The sum is placed back into <tt class="literal">$sum</tt>, and on throughthe rest of the parameters. Finally, the last line returns<tt class="literal">$sum</tt> to the caller.</p><p>There's a potential bug in this subroutine, depending upon howyou think of things. Suppose that this subroutine was called with anempty parameter list (as we considered with the rewritten subroutine<tt class="literal">&amp;max</tt> in the chapter text). In that case,<tt class="literal">$sum</tt> would be <tt class="literal">undef</tt>, and thatwould be the return value. But in this subroutine, it would probablybe "more correct" to return <tt class="literal">0</tt> as thesum of the empty list, rather than <tt class="literal">undef</tt>. (Ofcourse, if you wished to distinguish the sum of an empty list fromthe sum of, say, <tt class="literal">(3, -5, 2)</tt>, returning<tt class="literal">undef</tt> would be the right thing to do.)</p><p>If you don't want a possibly undefined return value, though,it's easy to remedy: simply initialize <tt class="literal">$sum</tt>to zero rather than using the default of <tt class="literal">undef</tt>:</p><blockquote><pre class="code">my $sum = 0;</pre></blockquote><p>Now the subroutine will always return a number, even if the parameterlist were empty.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code"># Remember to include &amp;total from previous exercise!print "The numbers from 1 to 1000 add up to ", &amp;total(1..1000), ".\n";</pre></blockquote><p>Note that we can't call the subroutine from inside thedouble-quoted string,<a href="#FOOTNOTE-383">[383]</a> so the subroutine call is another separate item beingpassed to <tt class="literal">print</tt>. The total should be<tt class="literal">500500</tt>, a nice round number. And itshouldn't take any noticeable time at all to run this program;passing a parameter list of 1000 values is an everyday task for Perl.</p><blockquote class="footnote"> <a name="FOOTNOTE-383" /><p>[383]We can't do this withoutadvanced trickiness, that is. It's rare to find anything thatyou <em class="emphasis">absolutely</em> can't do in Perl.</p></blockquote></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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.2. Answers to Chapter 3 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.4. Answers to Chapter 5 Exercises</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 + -