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

📄 ch02_08.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>List Values and Arrays (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="List Values and Arrays"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch02_07.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch02_01.htm">Chapter 2: Bits and Pieces</a></td><td align="right" valign="top" width="172"><a href="ch02_09.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">2.8. List Values and Arrays</h2><a name="INDEX-672"></a><a name="INDEX-673"></a><a name="INDEX-674"></a><a name="INDEX-675"></a><a name="INDEX-676"></a><a name="INDEX-677"></a><p>Now that we've talked about context, we can talk about list literalsand how they behave in context.  You've already seen some listliterals.  List literals are denoted by separating individual values bycommas (and enclosing the list in parentheses where precedence requiresit).  Because it (almost) never hurts to use extra parentheses, thesyntax diagram of a list value is usually indicated like this:<blockquote><pre class="programlisting">(<em class="replaceable">LIST</em>)</pre></blockquote>Earlier we said that <em class="replaceable">LIST</em> in a syntaxdescription indicates something that supplies list context to itsarguments, but a bare list literal itself is the one partial exceptionto that rule, in that it supplies a list context to its arguments onlywhen the list as a whole is in list context.  The value of a listliteral in list context is just the values of the arguments in theorder specified.  As a fancy sort of term in an expression, a listliteral merely pushes a series of temporary values onto Perl's stack,to be collected off the stack later by whatever operator wants thelist.<a name="INDEX-678"></a></p><p>In a scalar context, however, the list literal doesn't really behavelike a <em class="replaceable">LIST</em>, in that it doesn't supply list context to its values.Instead, it merely evaluates each of its arguments in scalar context,and returns the value of the final element.  That's because it'sreally just the C comma operator in disguise, which is a binary operatorthat always throws away the value on the left and returns the valueon the right.  In terms of what we discussed earlier, the left sideof the comma operator really provides a void context.  Because thecomma operator is left associative, if you have a series ofcomma-separated values, you always end up with the last valuebecause the final comma throws away whatever any previous commasproduced.  So, to contrast the two, the list assignment:<blockquote><pre class="programlisting">@stuff = ("one", "two", "three");</pre></blockquote>assigns the entire list value to array <tt class="literal">@stuff</tt>, but the scalarassignment:<blockquote><pre class="programlisting">$stuff = ("one", "two", "three");</pre></blockquote>assigns only the value "<tt class="literal">three</tt>" to variable<tt class="literal">$stuff</tt>.  Like the <tt class="literal">@files</tt> arraywe mentioned earlier the comma operator knows whether it is in ascalar or list context, and chooses its behavior accordingly.</p><p><a name="INDEX-679"></a><a name="INDEX-680"></a>It bears repeating that a list value is different from an array.A real array variable also knows its context, and in a list context,it would return its internal list of values just like a list literal.But in a scalar context it returns only the length of the array.The following assigns to <tt class="literal">$stuff</tt> the value 3:<blockquote><pre class="programlisting">@stuff = ("one", "two", "three");$stuff = @stuff;</pre></blockquote>If you expected it to get the value "<tt class="literal">three</tt>", you were probablymaking a false generalization by assuming that Perl uses the commaoperator rule to throw away all but one of the temporary values that<tt class="literal">@stuff</tt> put on the stack.  But that's not how it works.  The<tt class="literal">@stuff</tt> array never put all its values on the stack.  It never putany of its values on the stack, in fact.  It only put one value, thelength of the array, because it <em class="emphasis">knew</em> it was in scalar context.  Noterm or operator in scalar context will ever put a list on the stack.Instead, it will put one scalar on the stack, whatever it feels like,which is unlikely to be the last value of the list it <em class="emphasis">would</em> havereturned in list context, because the last value is not likely to bethe most useful value in scalar context.  Got that?  (If not, you'dbetter reread this paragraph, because it's important.)</p><p><a name="INDEX-681"></a><a name="INDEX-682"></a>Now back to true <em class="replaceable">LIST</em>s, the onesthat do list context.  Until now we've pretended that list literalswere just lists of literals.  But just as a string literal mightinterpolate other substrings, a list literal can interpolate othersublists.  Any expression that returns values may be used within alist.  The values so used may be either scalar values or list values,but they all become part of the new list value because<em class="replaceable">LIST</em>s do automatic interpolation ofsublists.  That is, when a <em class="replaceable">LIST</em> isevaluated, each element of the list is evaluated in a list context,and the resulting list value is interpolated into<em class="replaceable">LIST</em> just as if each individual elementwere a member of <em class="replaceable">LIST</em>.  Thus arrays losetheir identity in a <em class="replaceable">LIST</em>.<a href="#FOOTNOTE-18">[18]</a> The list:<blockquote><pre class="programlisting">(@stuff,@nonsense,funkshun())</pre></blockquote></p><blockquote class="footnote"><a name="FOOTNOTE-18"></a><p>[18]Some people seem to think this is a problem, but it's not.  You canalways interpolate a reference to an array if you do not want it tolose its identity.  See<a href="ch08_01.htm">Chapter 8, "References"</a>.</p></blockquote><p><a name="INDEX-683"></a><a name="INDEX-684"></a><a name="INDEX-685"></a>contains the elements of <tt class="literal">@stuff</tt>, followed by theelements of <tt class="literal">@nonsense</tt>, followed by whatever valuesthe subroutine <tt class="literal">&amp;funkshun</tt> decides to return whencalled in list context.  Note that any or all of these might haveinterpolated a null (empty) list, in which case it's as if no array orfunction call had been interpolated at that point.  The null listitself is represented by the literal <tt class="literal">()</tt>.  As with anull array, which interpolates as a null list and is thereforeeffectively ignored, interpolating the null list into another list hasno effect.  Thus, <tt class="literal">((),(),())</tt> is equivalent to<tt class="literal">()</tt>.</p><p>A corollary to this rule is that you may place an optional comma at the endof any list value.  This makes it easy to come back later and add moreelements after the last one:<blockquote><pre class="programlisting">@releases = (    "alpha",    "beta",    "gamma",);</pre></blockquote><a name="INDEX-686"></a></p><p><a name="INDEX-687"></a><a name="INDEX-688"></a>Or you can do away with the commas entirely: another way to specify aliteral list is with the <tt class="literal">qw</tt> (quote words) syntax we mentioned earlier.This construct is equivalent to splitting a single-quoted string onwhitespace.  For example:<blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -