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

📄 ch03_07.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Perl's Favorite Default: $_ (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="ch03_06.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="ch03_08.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.7. Perl's Favorite Default: $_</h2><p><a name="INDEX-295" /> <a name="INDEX-296" />If you omitthe control variable from the beginning of the <tt class="literal">foreach</tt> loop, Perl uses its favoritedefault variable, <tt class="literal">$_</tt>. This is(mostly) just like any other scalar variable, except for its unusualname. For example:</p><blockquote><pre class="code">foreach (1..10) {  # Uses $_ by default  print "I can count to $_!\n";}</pre></blockquote><p>Although this isn't Perl's only default by a long shot,it's Perl's most common default. We'll see manyother cases in which Perl will automatically use <tt class="literal">$_</tt> when you don't tell it to use someother variable or value, thereby saving the programmer from the heavylabor of having to think up and type a new variable name. So as notto keep you in suspense, one of those cases is <tt class="literal">print</tt>, which will print <tt class="literal">$_</tt> if given no other argument:</p><blockquote><pre class="code">$_ = "Yabba dabba doo\n";print;  # prints $_ by default</pre></blockquote><a name="lperl3-CHP-3-SECT-7.1" /><div class="sect2"><h3 class="sect2">3.7.1. The reverse Operator</h3><p>The <tt class="literal">reverse</tt><a name="INDEX-297" /> operator takes a list of values (whichmay come from an array) and returns the list in the opposite order.So if you were disappointed that the range operator, <tt class="literal">..</tt>, only counts upwards, this is the way tofix it:</p><blockquote><pre class="code">@fred = 6..10;@barney = reverse(@fred); # gets 10, 9, 8, 7, 6@wilma = reverse 6..10;   # gets the same thing, without the other array@fred = reverse @fred;    # puts the result back into the original array</pre></blockquote><p>The last line is noteworthy because it uses <tt class="literal">@fred</tt> twice. Perl always calculates thevalue being assigned (on the right) before it begins the actualassignment.</p><p>Remember that <tt class="literal">reverse</tt> returns thereversed list; it doesn't affect its arguments. If the returnvalue isn't assigned anywhere, it's useless:</p><blockquote><pre class="code">reverse @fred;         # WRONG - doesn't change @fred@fred = reverse @fred; # that's better</pre></blockquote></div><a name="lperl3-CHP-3-SECT-7.2" /><div class="sect2"><h3 class="sect2">3.7.2. The sort Operator</h3><p>The <tt class="literal">sort</tt><a name="INDEX-298" /> operator takes a list of values(which may come from an array) and sorts them in the internalcharacter ordering. For ASCII strings, that would be<a name="INDEX-299" />ASCIIbetical order. Of course,ASCII is a strange place where all of the capital letters come beforeall of the lowercase letters, where the numbers come before theletters, and the punctuation marks -- well, those are here, there,and everywhere. But sorting in ASCII order is just the<em class="emphasis">default</em> behavior; we'll see in <a href="ch15_01.htm">Chapter 15, "Strings and Sorting"</a>, <em class="emphasis">Strings and Sorting</em>, howto sort in whatever order you'd like:</p><blockquote><pre class="code">@rocks = qw/ bedrock slate rubble granite /;@sorted = sort(@rocks);      # gets bedrock, granite, rubble, slate@back = reverse sort @rocks; # these go from slate to bedrock@rocks = sort @rocks;        # puts sorted result back into @rocks@numbers = sort 97..102;     # gets 100, 101, 102, 97, 98, 99</pre></blockquote><p>As you can see from that last example, sorting numbers as if theywere strings may not give useful results. But, of course, any stringthat starts with <tt class="literal">1</tt> has to sortbefore any string that starts with <tt class="literal">9</tt>, according to the default sorting rules.And like what happened with <tt class="literal">reverse</tt>, the arguments themselvesaren't affected. If you want to sort an array, you must storethe result back into that array:</p><blockquote><pre class="code">sort @rocks;          # WRONG, doesn't modify @rocks@rocks = sort @rocks; # Now the rock collection is in order</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="ch03_06.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="ch03_08.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.6. The foreach Control Structure</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">3.8. Scalar and List Context</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 + -