ch03_02.htm

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

HTM
74
字号
<html><head><title>Special Array Indices (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_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="ch03_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.2. Special Array Indices</h2><p>If you store into an <a name="INDEX-266" />array element that is beyond the end ofthe array, the array is automatically extended asneeded -- there's no limit on its length, as long asthere's available memory for Perl to use. If interveningelements need to be created, they'll be created as <tt class="literal">undef</tt> values.</p><blockquote><pre class="code">$rocks[0] = 'bedrock';      # One element...$rocks[1] = 'slate';        # another...$rocks[2] = 'lava';         # and another...$rocks[3] = 'crushed rock'; # and another...$rocks[99] = 'schist';      # now there are 95 undef elements</pre></blockquote><p>Sometimes, you need to find out the last element index in an array.For the array of <tt class="literal">rocks</tt> thatwe've just been using, the last element index is <tt class="literal">$#rocks</tt>.<a href="#FOOTNOTE-70">[70]</a> That'snot the same as the number of elements, though, because there'san element number zero. As seen in the code snippet below, it'sactually possible to assign to this value to change the size of thearray, although this is rare in practice.<a href="#FOOTNOTE-71">[71]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-70" /><p>[70]Blame this uglysyntax on the C shell. Fortunately, we don't have to look atthis very often in the real world.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-71" /><p>[71]This isvery infrequently done to "pre-size" an array, so thatPerl won't need to allocate memory in many small chunks as anarray grows. See the Perl documentation for more information, in theunlikely case that you need this.</p> </blockquote><blockquote><pre class="code">$end = $#rocks;                  # 99, which is the last element's index$number_of_rocks = $end + 1;     # okay, but we'll see a better way later$#rocks = 2;                     # Forget all rocks after 'lava'$#rocks = 99;                    # add 97 undef elements (the forgotten rocks are                                 # gone forever)$rocks[ $#rocks ] = 'hard rock'; # the last rock</pre></blockquote><p>Using the <tt class="literal">$#name</tt> value as anindex, like that last example, happens often enough that Larry hasprovided a shortcut: <a name="INDEX-267" />negative array indices count from theend of the array. But don't get the idea that these indices"wrap around." If you've got three elements in thearray, the valid negative indices are <tt class="literal">-1</tt> (the last element), <tt class="literal">-2</tt> (the middle element), and <tt class="literal">-3</tt> (the first element). In the real world,nobody seems to use any of these except <tt class="literal">-1</tt>, though.</p><blockquote><pre class="code">$rocks[ -1 ] = 'hard rock'; # easier way to do that last example above$dead_rock = $rocks[-100];  # gets 'bedrock'$rocks[ -200 ] = 'crystal'; # fatal error!</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="ch03_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="ch03_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3. Lists and Arrays </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.3. List Literals</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 + -
显示快捷键?