📄 ch03_01.htm
字号:
<html><head><title>Lists and Arrays (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="ch02_13.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>
<h1 class="chapter">Chapter 3. Lists and Arrays </h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
<p> <a href="#lperl3-CHP-3-SECT-1">Accessing Elements of an Array</a><br />
<a href="ch03_02.htm">Special Array Indices</a><br />
<a href="ch03_03.htm">List Literals</a><br />
<a href="ch03_04.htm">List Assignment</a><br />
<a href="ch03_05.htm">Interpolating Arrays into Strings</a><br />
<a href="ch03_06.htm">The foreach Control Structure</a><br />
<a href="ch03_07.htm">Perl's Favorite Default: $_</a><br />
<a href="ch03_08.htm">Scalar and List Context</a><br />
<a href="ch03_09.htm"><STDIN> in List Context</a><br />
<a href="ch03_10.htm">Exercises</a><br /></p></div>
<p><a name="INDEX-259" /></a>
<a name="INDEX-260" /></a>If a
scalar was the "singular" in Perl, as we described them
at the beginning of <a href="ch02_01.htm">Chapter 2, "Scalar Data"</a>, the
"plural" in Perl is represented by lists and arrays.
</p>
<p>A <em class="emphasis">list</em> is an ordered collection of scalars. An
<em class="emphasis">array</em> is a variable that contains a list. In
Perl, the two terms are often used as if they're
interchangeable. But, to be accurate, the list is the data, and the
array is the variable. You can have a list value that isn't in
an array, but every array variable holds a list (although that list
may be empty). <a href="ch03_01.htm#lperl3-CHP-3-FIG-1">Figure 3-1</a> represents a list,
whether it's stored in an array or not.
</p>
<a name="lperl3-CHP-3-FIG-1" /></a><div class="figure"><img height="156" alt="Figure 3-1" src="figs/lrnp_0301.gif" width="180" /></div><h4 class="objtitle">Figure 3-1. A list with five elements</h4>
<p>Each <em class="emphasis">element</em><a name="INDEX-261" /></a>
of an array or list is a separate scalar variable with an independent
scalar value. These values are ordered -- that is, they have a
particular sequence from the first to the last element. The elements
of an array or list are
<em class="emphasis">indexed</em><a name="INDEX-262" /></a> by small integers starting at
zero<a href="#FOOTNOTE-67">[67]</a> and counting by ones, so the first element of any array
or list is always element zero.
</p><blockquote class="footnote"> <a name="FOOTNOTE-67" /></a><p>[67]Array and list indices always start at zero in
Perl, unlike in some other languages. In early Perl, it was possible
to change the starting number of array and list indexing (not for
just one array or list, but for all of them at once!). Larry later
realized that this was a misfeature, and its (ab)use is now strongly
discouraged. But, if you're terminally curious, look up the
<tt class="literal">$[</tt>variable in
the <em class="emphasis">perlvar</em>manpage.</p>
</blockquote>
<p>Since each element is an independent scalar value, a list or array
may hold numbers, strings, <tt class="literal">undef</tt> values, or any
mixture of different scalar values. Nevertheless, it's most
common to have all elements of the same type, such as a list of book
titles (all strings) or a list of cosines (all numbers).
</p>
<p>Arrays and lists can have any number of elements. The smallest one
has no elements, while the largest can fill all of available memory.
Once again, this is in keeping with Perl's philosophy of
"no unnecessary limits."
</p>
<div class="sect1"><a name="lperl3-CHP-3-SECT-1" /></a>
<h2 class="sect1">3.1. Accessing Elements of an Array</h2>
<p>If you've used arrays in another language, you won't be
surprised to find that Perl provides a way to
<a name="INDEX-263" /></a>subscript an
array in order to refer to an element by a numeric index.
</p>
<p>The <a name="INDEX-264" /></a>
<a name="INDEX-265" /></a>array elements are numbered using
sequential integers, beginning at zero and increasing by one for each
element, like this:
</p>
<blockquote><pre class="code">$fred[0] = "yabba";
$fred[1] = "dabba";
$fred[2] = "doo";</pre></blockquote>
<p>The array name itself (in this case, <tt class="literal">"fred"</tt>) is
from a completely separate namespace than scalars use; you could have
a scalar variable named <tt class="literal">$fred</tt> in the same program,
and Perl will treat them as different things, and wouldn't be
confused.<a href="#FOOTNOTE-68">[68]</a>
(Your maintenance programmer might be confused, though, so
don't capriciously make all of your variable names the same!)
</p><blockquote class="footnote"> <a name="FOOTNOTE-68" /></a><p>[68]The syntax is always
unambiguous -- tricky perhaps, but unambiguous.</p> </blockquote>
<p>You can use an array element like <tt class="literal">$fred[2]</tt> in
every place<a href="#FOOTNOTE-69">[69]</a> where
you could use any other scalar variable like
<tt class="literal">$fred</tt>. For example, you can get the value from an
array element or change that value by the same sorts of expressions
we used in the previous chapter:
</p><blockquote class="footnote"> <a name="FOOTNOTE-69" /></a><p>[69]Well, almost. The most notable exception
is that the control variable of a <tt class="literal">foreach</tt> loop,
which we'll see later in this chapter, must be a simple scalar.
And there are others, like the "indirect object slot" and
"indirect filehandle slot" for <tt class="literal">print
</tt>and <tt class="literal">printf</tt>.</p> </blockquote>
<blockquote><pre class="code">print $fred[0];
$fred[2] = "diddley";
$fred[1] .= "whatsis";</pre></blockquote>
<p>Of course, the subscript may be any expression that gives a numeric
value. If it's not an integer already, it'll
automatically be truncated to the next lower integer:
</p>
<blockquote><pre class="code">$number = 2.71828;
print $fred[$number - 1]; # Same as printing $fred[1]</pre></blockquote>
<p>If the subscript indicates an element that would be beyond the end of
the array, the corresponding value will be <tt class="literal">undef</tt>. This is just as with ordinary
scalars; if you've never stored a value into the variable,
it's <tt class="literal">undef</tt>.
</p>
<blockquote><pre class="code">$blank = $fred[ 142_857 ]; # unused array element gives undef
$blanc = $mel; # unused scalar $mel also gives undef</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="ch02_13.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.13. Exercises</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.2. Special Array Indices</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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -