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

📄 ch04_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><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:30:52Z"><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="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch03_12.htm"TITLE="3.11. Program: hopdelta"><LINKREL="next"HREF="ch04_02.htm"TITLE="4.1. Specifying a List In Your Program"></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="ch03_12.htm"TITLE="3.11. Program: hopdelta"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.11. Program: hopdelta"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_02.htm"TITLE="4.1. Specifying a List In Your Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.1. Specifying a List In Your Program"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch04-42614">4. Arrays</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch04-12050"TITLE="4.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch04_02.htm"TITLE="4.1. Specifying a List In Your Program">Specifying a List In Your Program</A><BR><ACLASS="sect1"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas">Printing a List with Commas</A><BR><ACLASS="sect1"HREF="ch04_04.htm"TITLE="4.3. Changing Array Size">Changing Array Size</A><BR><ACLASS="sect1"HREF="ch04_05.htm"TITLE="4.4. Doing Something with Every Element in a List">Doing Something with Every Element in a List</A><BR><ACLASS="sect1"HREF="ch04_06.htm"TITLE="4.5. Iterating Over an Array by Reference">Iterating Over an Array by Reference</A><BR><ACLASS="sect1"HREF="ch04_07.htm"TITLE="4.6. Extracting Unique Elements from a List">Extracting Unique Elements from a List</A><BR><ACLASS="sect1"HREF="ch04_08.htm"TITLE="4.7. Finding Elements in One Array but Not Another">Finding Elements in One Array but Not Another</A><BR><ACLASS="sect1"HREF="ch04_09.htm"TITLE="4.8. Computing Union, Intersection, or Difference of Unique Lists">Computing Union, Intersection, or Difference of Unique Lists</A><BR><ACLASS="sect1"HREF="ch04_10.htm"TITLE="4.9. Appending One Array to Another">Appending One Array to Another</A><BR><ACLASS="sect1"HREF="ch04_11.htm"TITLE="4.10. Reversing an Array">Reversing an Array</A><BR><ACLASS="sect1"HREF="ch04_12.htm"TITLE="4.11. Processing Multiple Elements of an Array">Processing Multiple Elements of an Array</A><BR><ACLASS="sect1"HREF="ch04_13.htm"TITLE="4.12. Finding the First List Element That Passes a Test">Finding the First List Element That Passes a Test</A><BR><ACLASS="sect1"HREF="ch04_14.htm"TITLE="4.13. Finding All Elements in an Array Matching Certain Criteria">Finding All Elements in an Array Matching Certain Criteria</A><BR><ACLASS="sect1"HREF="ch04_15.htm"TITLE="4.14. Sorting an Array Numerically">Sorting an Array Numerically</A><BR><ACLASS="sect1"HREF="ch04_16.htm"TITLE="4.15. Sorting a List by Computable Field">Sorting a List by Computable Field</A><BR><ACLASS="sect1"HREF="ch04_17.htm"TITLE="4.16. Implementing a Circular List">Implementing a Circular List</A><BR><ACLASS="sect1"HREF="ch04_18.htm"TITLE="4.17. Randomizing an Array">Randomizing an Array</A><BR><ACLASS="sect1"HREF="ch04_19.htm"TITLE="4.18. Program: words">Program: words</A><BR><ACLASS="sect1"HREF="ch04_20.htm"TITLE="4.19. Program: permute">Program: permute</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>Works of art, in my opinion, are the only objects in the material universe to possess internal order, and that is why, though I don't believe that only art matters, I do believe in Art for Art's sake.</I></P><PCLASS="attribution"ALIGN="right">-&nbsp;E.M. Forster </P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch04-12050">4.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch04-idx-1000006569-0"></A>If you are asked about the contents of your pockets, or the names of the last three presidents, or how to get to the highway, you recite a list: you name one thing after another in a particular order. <ACLASS="indexterm"NAME="ch04-idx-1000006571-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006571-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006571-2"></A>Lists are part of your conception of the world. With Perl's powerful list- and array-handling primitives, you can translate this world view directly into code.</P><PCLASS="para">In this chapter, we'll use the terms <ICLASS="firstterm">list</I> and <ICLASS="firstterm">array</I> as the Perl language thinks of them. Take <CODECLASS="literal">(&quot;Reagan&quot;,</CODE> <CODECLASS="literal">&quot;Bush&quot;,</CODE> <CODECLASS="literal">&quot;Clinton&quot;)</CODE>; that's a <EMCLASS="emphasis">list</EM> of the last three American presidents, in order. To store that list into a variable, use an <EMCLASS="emphasis">array</EM>, as in <CODECLASS="literal">@presidents</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">(&quot;Reagan&quot;,</CODE> <CODECLASS="literal">&quot;Bush&quot;,</CODE> <CODECLASS="literal">&quot;Clinton&quot;)</CODE>. Both are ordered groups of scalar values; the difference is that an array is a named variable, one whose array length can be directly changed, whereas a list is a more ephemeral notion. You might think of an array as a variable and a list as the values it contains.</P><PCLASS="para">This distinction may seem arbitrary, but operations that modify the length of these groupings (like <CODECLASS="literal">push</CODE> and <CODECLASS="literal">pop</CODE>) require a proper array and not merely a list. Think of the difference between <CODECLASS="literal">$a</CODE> and <CODECLASS="literal">4</CODE>. You can say <CODECLASS="literal">$a++</CODE> but not <CODECLASS="literal">4++</CODE>. Likewise, you can say <CODECLASS="literal">pop(@a)</CODE> but not <CODECLASS="literal">pop</CODE> <CODECLASS="literal">(1,2,3)</CODE>.</P><PCLASS="para">The most important thing to glean from this is that Perl's lists and arrays are both ordered groupings of scalars. Operators and functions that work on lists or arrays are designed to provide faster or more convenient access to the elements than manual access would provide. Since few actually deal with modifying the array's length, you can usually use arrays and lists interchangeably.</P><PCLASS="para">You can't use nested parentheses to create a list of lists. If you try that in Perl, your lists get <ICLASS="firstterm">flattened</I>, meaning that both these lines are equivalent: <ACLASS="indexterm"NAME="ch04-idx-1000006574-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006574-1"></A></P><PRECLASS="programlisting">@nested = (&quot;this&quot;, &quot;that&quot;, &quot;the&quot;, &quot;other&quot;);@nested = (&quot;this&quot;, &quot;that&quot;, (&quot;the&quot;, &quot;other&quot;));</PRE><PCLASS="para">Why doesn't Perl (usefully) just support nested lists directly? Although partially for historical reasons, this easily allows for operations (like <CODECLASS="literal">print</CODE> or <CODECLASS="literal">sort</CODE>) that work on arbitrarily long lists of arbitrary contents.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch04-idx-1000006573-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006573-1"></A>What happens if you want a more complex data structure, such as an array of arrays or an array of hashes? Remember that scalars aren't restricted to containing just numbers or strings; they can also hold references. Complex (multilevel) data structures in Perl are always put together using references. Therefore, what appear to be "two-dimensional arrays" or "arrays of arrays" are always implemented as arrays of array <EMCLASS="emphasis">references</EM>, in the same way that two-dimensional arrays in C can be arrays of pointers to arrays.</P><PCLASS="para">Most recipes in this chapter don't care what you keep in your arrays; for example, the problem of merging two arrays is the same whether the arrays contains strings, numbers, or references. Some problems are intrinsically tied to the contents of your arrays; recipes for those are in <ACLASS="xref"HREF="ch11_01.htm"TITLE="References and Records">Chapter 11, <CITECLASS="chapter">References and Records</CITE></A>. This chapter's recipes deal with generic arrays.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch04-idx-1000006575-0"></A>Let's have some more terminology. The scalar items in an array or list are called <EMCLASS="emphasis">elements</EM>, which you access by specifying their position, or<ACLASS="indexterm"NAME="ch04-idx-1000006576-0"></A> <EMCLASS="emphasis">index</EM>. Indices in Perl start at <CODECLASS="literal">0</CODE>. So, given this list:</P><PRECLASS="programlisting">@tune = ( &quot;The&quot;, &quot;Star-Spangled&quot;, &quot;Banner&quot; );</PRE><PCLASS="para"><CODECLASS="literal">&quot;The&quot;</CODE> is in the first position, but you'd access it as <CODECLASS="literal">$tune[0]</CODE>. <CODECLASS="literal">&quot;Star-Spangled&quot;</CODE> is in the second position, but you'd access it as <CODECLASS="literal">$tune[1]</CODE>. This structure is doubly justified: the contrariness of computers, whose first representable number is 0, and the contrariness of language designers, who chose 0 because it is an <EMCLASS="emphasis">offset</EM> into the array, not the <EMCLASS="emphasis">ordinal</EM> number of the element.</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="ch03_12.htm"TITLE="3.11. Program: hopdelta"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.11. Program: hopdelta"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_02.htm"TITLE="4.1. Specifying a List In Your Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.1. Specifying a List In Your Program"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.11. Program: hopdelta</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.1. Specifying a List In Your Program</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 + -