appa_02.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 92 行
HTM
92 行
<html><head><title>Answers to Chapter 3 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 & 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_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="appa_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.2. Answers to Chapter 3 Exercises</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z@lines = <STDIN>;@reverse_lines = reverse @lines;print @reverse_lines;</pre></blockquote><p>...or, even more simply:</p><blockquote><pre class="code">print "Enter some lines, then press Ctrl-D:\n";print reverse <STDIN>;</pre></blockquote><p>Most Perl programmers would prefer the second one, as long as youdon't need to keep the list of lines around for later use.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">@names = qw/ fred betty barney dino wilma pebbles bamm-bamm /;print "Enter some numbers from 1 to 7, one per line, then press Ctrl-D:\n";chomp(@numbers = <STDIN>);foreach (@numbers) { print "$names[ $_ - 1 ]\n";}</pre></blockquote><p>We have to subtract one from the index number so that the user cancount from 1 to 7 even though the array is indexed from 0 to 6.Another way to accomplish this would be to have a dummy item in the<tt class="literal">@names</tt> array, like this:</p><blockquote><pre class="code">@names = qw/ dummy_item fred betty barney dino wilma pebbles bamm-bamm /;</pre></blockquote><p>Give yourself extra credit if you checked to make sure that theuser's choice of index was in fact in the range 1 to 7.</p></li><li><p>Here's one way to do it, if you want the output all on one line:</p><blockquote><pre class="code">chomp(@lines = <STDIN>);@sorted = sort @lines;print "@sorted\n";</pre></blockquote><p>...or, to get the output on separate lines:</p><blockquote><pre class="code">print sort <STDIN>;</pre></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_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="appa_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A. Exercise Answers</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.3. Answers to Chapter 4 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 © 2002</a> O'Reilly & 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 + -
显示快捷键?