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

📄 ch04_04.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 4.3. Changing Array Size (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:31:12Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch04_01.htm"TITLE="4. Arrays"><LINKREL="prev"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas"><LINKREL="next"HREF="ch04_05.htm"TITLE="4.4. Doing Something with Every Element in a List"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.2. Printing a List with Commas"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch04_01.htm"TITLE="4. Arrays"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_05.htm"TITLE="4.4. Doing Something with Every Element in a List"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.4. Doing Something with Every Element in a List"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch04-24943">4.3. Changing Array Size</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-297">Problem <ACLASS="indexterm"NAME="ch04-idx-1000006617-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006617-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006617-2"></A></A></H3><PCLASS="para">You want to enlarge or truncate an array. For example, you might truncate an array of employees that's already sorted by salary to list the five highest-paid employees. Or, if you know how big your array will get and that it will grow piecemeal, it's more efficient to get memory for it in one step by enlarging it just once than it is to keep <CODECLASS="literal">push</CODE>ing values onto the end.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-303">Solution</A></H3><PCLASS="para">Assign to <CODECLASS="literal">$#ARRAY</CODE><ACLASS="indexterm"NAME="ch04-idx-1000006623-0"></A>:</P><PRECLASS="programlisting"># grow or shrink @ARRAY$#ARRAY = $NEW_LAST_ELEMENT_INDEX_NUMBER;</PRE><PCLASS="para">Assigning to an element past the end automatically extends the array:</P><PRECLASS="programlisting">$ARRAY[$NEW_LAST_ELEMENT_INDEX_NUMBER] = $VALUE;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-317">Discussion</A></H3><PCLASS="para"><CODECLASS="literal">$#ARRAY</CODE> is the number of <ACLASS="indexterm"NAME="ch04-idx-1000006629-0"></A>the last valid index in <CODECLASS="literal">@ARRAY</CODE>. If we assign it a number smaller than its current value, we truncate the array. Truncated elements are lost forever. If we assign <CODECLASS="literal">$#ARRAY</CODE> a number larger than its current value, the array grows. New elements have the undefined value.</P><PCLASS="para"><CODECLASS="literal">$#ARRAY</CODE> is not <CODECLASS="literal">@ARRAY</CODE>, though. Although <CODECLASS="literal">$#ARRAY</CODE> is the last valid index in the array, <CODECLASS="literal">@ARRAY</CODE> (in scalar context, as when treated as a number) is the <EMCLASS="emphasis">number</EM> of elements. <CODECLASS="literal">$#ARRAY</CODE> is one less than <CODECLASS="literal">@ARRAY</CODE> because array indices start at 0.</P><PCLASS="para">Here's some code that uses both:</P><PRECLASS="programlisting">sub what_about_that_array {    print &quot;The array now has &quot;, scalar(@people), &quot; elements.\n&quot;;    print &quot;The index of the last element is $#people.\n&quot;;    print &quot;Element #3 is `$people[3]'.\n&quot;;}@people = qw(Crosby Stills Nash Young);what_about_that_array();</PRE><PCLASS="para">prints:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The array now has 4 elements.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The index of the last element is 3.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Element #3 is `Young'.</I></CODE></B></CODE></PRE><PCLASS="para">whereas:</P><PRECLASS="programlisting">$#people--;what_about_that_array();</PRE><PCLASS="para">prints:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The array now has 3 elements.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The index of the last element is 2.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Element #3 is `'.</I></CODE></B></CODE></PRE><PCLASS="para">Element #3 disappeared when we shortened the array. If we'd used the <CODECLASS="literal">-w</CODE> flag on this program, Perl would also have warned "use of uninitialized value" because <CODECLASS="literal">$people[3]</CODE> is undefined.</P><PRECLASS="programlisting">$#people = 10_000;what_about_that_array();</PRE><PCLASS="para">prints:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The array now has 10001 elements.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The index of the last element is 10000.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Element #3 is `'.</I></CODE></B></CODE><CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch04-idx-1000006625-0"></A></PRE><PCLASS="para">The <CODECLASS="literal">&quot;Young&quot;</CODE> element is now gone forever. Instead of assigning to <CODECLASS="literal">$#people</CODE>, we could have said:</P><PRECLASS="programlisting">$people[10_000] = undef;</PRE><PCLASS="para">Perl arrays are not sparse. In other words, if you have a 10,000th element, you must have the 9,999 other elements, too. They may be undefined, but they still take up memory. For this reason, <CODECLASS="literal">$array[time]</CODE>, or any other construct that uses a very large integer as an array index, is a bad idea. Use a hash instead.</P><PCLASS="para">We have to say <CODECLASS="literal">scalar</CODE> <CODECLASS="literal">@array</CODE> in the <CODECLASS="literal">print</CODE> because Perl gives list context to (most) functions' arguments, but we want <CODECLASS="literal">@array</CODE> in scalar context.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-387">See Also</A></H3><PCLASS="para">The discussion of the <CODECLASS="literal">$#ARRAY</CODE> notation in <EMCLASS="emphasis">perldata </EM>(1), also explained in the <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.4">"List Values and Arrays"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A><ACLASS="indexterm"NAME="ch04-idx-1000006619-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006619-1"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.2. Printing a List with Commas"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_05.htm"TITLE="4.4. Doing Something with Every Element in a List"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.4. Doing Something with Every Element in a List"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">4.2. Printing a List with Commas</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">4.4. Doing Something with Every Element in a List</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -