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

📄 ch02_08.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<pre class="programlisting">@froots = qw(    apple       banana      carambola    coconut     guava       kumquat    mandarin    nectarine   peach    pear        persimmon   plum);</pre></blockquote>(Note that those parentheses are behaving as quote characters, notordinary parentheses.  We could just as easily have picked anglebrackets or braces or slashes.  But parens are pretty.)<a name="INDEX-689"></a></p><p>A list value may also be subscripted like a normal array.  You must putthe list in parentheses (real ones) to avoid ambiguity.  Though it'soften used to fetch a single value out of a list, it's reallya slice of the list, so the syntax is:<blockquote><pre class="programlisting">(LIST)[LIST]</pre></blockquote>Examples:<blockquote><pre class="programlisting"># Stat returns list value.$modification_time = (stat($file))[9];# SYNTAX ERROR HERE.$modification_time = stat($file)[9];  # OOPS, FORGOT PARENS# Find a hex digit.$hexdigit = ('a','b','c','d','e','f')[$digit-10];# A "reverse comma operator".return (pop(@foo),pop(@foo))[0];# Get multiple values as a slice.($day, $month, $year) = (localtime)[3,4,5];</pre></blockquote></p><h3 class="sect2">2.8.1. List Assignment</h3><a name="INDEX-690"></a><a name="INDEX-691"></a><p><a name="INDEX-692"></a>A list may be assigned to only if each element of the list isitself legal to assign to:<blockquote><pre class="programlisting">($a, $b, $c) = (1, 2, 3);($map{red}, $map{green}, $map{blue}) = (0xff0000, 0x00ff00, 0x0000ff);</pre></blockquote>You may assign to <tt class="literal">undef</tt> in a list.  This is useful for throwingaway some of the return values of a function:<blockquote><pre class="programlisting">($dev, $ino, undef, undef, $uid, $gid) = stat($file);</pre></blockquote>The final list element may be an array or a hash:<blockquote><pre class="programlisting">($a, $b, @rest) = split;my ($a, $b, %rest) = @arg_list;</pre></blockquote>You can actually put an array or hash anywhere in the list you assignto, but the first array or hash in the list will soak up all theremaining values, and anything after it will be set to the undefinedvalue.  This may be useful in a <tt class="literal">local</tt> or<tt class="literal">my</tt>, where you probably want the arrays initializedto be empty anyway.</p><p>You can even assign to the empty list:<blockquote><pre class="programlisting">() = funkshun();</pre></blockquote>That ends up calling your function in list context, but discardingthe return values.  If you had just called the function without anassignment, it would have instead been called in void context, whichis a kind of scalar context, and might have caused the function tobehave completely differently.<a name="INDEX-693"></a></p><p><a name="INDEX-694"></a>List assignment in scalar context returns the number of elementsproduced by the expression on the <em class="emphasis">right</em> side of the assignment:<blockquote><pre class="programlisting">$x = ( ($a, $b) = (7,7,7) );    # set $x to 3, not 2$x = ( ($a, $b) = funk() );     # set $x to funk()'s return count$x = ( () = funk() );           # also set $x to funk()'s return count</pre></blockquote>This is handy when you want to do a list assignment in a Booleancontext, because most list functions return a null list when finished,which when assigned produces a 0, which is interpreted as false.Here's how you might use it in a <tt class="literal">while</tt> statement:<blockquote><pre class="programlisting">while (($login, $password) = getpwent) {    if (crypt($login, $password) eq $password) {        print "$login has an insecure password!\n";    }}</pre></blockquote><a name="INDEX-695"></a></p><h3 class="sect2">2.8.2. Array Length</h3><p><a name="INDEX-696"></a><a name="INDEX-697"></a><a name="INDEX-698"></a><a name="INDEX-699"></a>You may find the number of elements in the array <tt class="literal">@days</tt> by evaluating<tt class="literal">@days</tt> in a scalar context, such as:<blockquote><pre class="programlisting">@days + 0;      # implicitly force @days into a scalar contextscalar(@days)   # explicitly force @days into a scalar context</pre></blockquote>Note that this only works for arrays.  It does not work for list valuesin general.  As we mentioned earlier, a comma-separated list evaluatedin scalar context returns the last value, like the C comma operator.But because you almost never actually need to know the length of a listin Perl, this is not a problem.</p><p><a name="INDEX-700"></a><a name="INDEX-701"></a><a name="INDEX-702"></a><a name="INDEX-703"></a>Closely related to the scalar evaluation of<tt class="literal">@days</tt> is <tt class="literal">$#days</tt>.  This willreturn the subscript of the last element of the array, or one lessthan the length, since there is (ordinarily) a 0th element.  Assigningto <tt class="literal">$#days</tt> changes the length of the array.Shortening an array by this method destroys intervening values.  Youcan gain some measure of efficiency by pre-extending an array that isgoing to get big. (You can also extend an array by assigning to anelement beyond the end of the array.) You can truncate an array downto nothing by assigning the null list <tt class="literal">()</tt> to it.The following two statements are equivalent:<blockquote><pre class="programlisting">@whatever = ();$#whatever = -1;</pre></blockquote>And the following is always true:<blockquote><pre class="programlisting">scalar(@whatever) == $#whatever + 1;</pre></blockquote>Truncating an array does not recover its memory.  You have to<tt class="literal">undef(@whatever)</tt> to free its memory back to your process's memorypool.  You probably can't free it all the way back to your system'smemory pool, because few operating systems support this.<a name="INDEX-704"></a></p><a name="INDEX-705"></a><a name="INDEX-706"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><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="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></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><tr><td align="left" valign="top" width="172">2.7. Context</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">2.9. Hashes</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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