📄 ch08_140.htm
字号:
<html><head><title>List::Util (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><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="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><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="ch08_139.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_141.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.140. List::Util</h2><p><a name="INDEX-1493" />Acollection of list-related subroutines. As of Perl 5.8, List::Util isincluded with the Perl source kit.</p><p>You can get the first item of a list (that matches the condition inBLOCK) like so:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse List::Util qw(first);my @ll = qw(one two three);my $fir = first { defined($_) } @ll;print "$fir\n"; # Prints 'one'</pre></blockquote><p>List::Util implements (but does not export) the following methods.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>first</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>first { <em class="replaceable">BLOCK</em> } @<em class="replaceable">list</em></pre><p>Evaluates a block of Perl code and sets <tt class="literal">$_</tt> to eachelement of the list in turn. If <em class="replaceable"><tt>BLOCK</tt></em> istrue, <tt class="literal">first</tt> returns the first element. If<em class="replaceable"><tt>BLOCK</tt></em> never returns true, or<tt class="literal">@</tt><em class="replaceable"><tt>list</tt></em> has no items,then <tt class="literal">first</tt> returns <tt class="literal">undef</tt>. Notethat <tt class="literal">first</tt> doesn't necessarilyreturn the first item in a list. Consider the following:</p><blockquote><pre class="code">my @ll = qw(1 2 3);my $fir = first { $_ > 1 } @ll;print "$fir\n"; # Prints '2', since as 2 is the first item # in BLOCK that's > 1</pre></blockquote></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>max</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>max @<em class="replaceable">list</em></pre><p>Returns the entry in the list with the highest numerical value. Ifthe list is empty, <tt class="literal">max</tt> returns<tt class="literal">undef</tt>:</p><blockquote><pre class="code">my @ll = qw(100 294 2 4 95 73);my $max_num = max @ll; print "$max_num\n"; # Prints '294'</pre></blockquote></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>maxstr</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>maxstr @<em class="replaceable">list</em></pre><p>Similar to <tt class="literal">max</tt>, except that<tt class="literal">maxstr</tt> treats all list items as strings.<tt class="literal">maxstr</tt> will return the "higheststring" as determined by the <tt class="literal">gt</tt>operator. As always, if list is empty, <tt class="literal">maxstr</tt>returns <tt class="literal">undef</tt>.</p><blockquote><pre class="code">my @ll = qw(1 3 5 nate Person pizza man carl_everett dinosaur);my $max_s = maxstr(@ll);print "$max_s\n"; # Prints 'pizza'</pre></blockquote></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>min</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>min @<em class="replaceable">list</em></pre><p><tt class="literal">R</tt>eturns the lowest numerical value. If the list isempty, <tt class="literal">min</tt> returns <tt class="literal">undef</tt>.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>minstr</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>minstr @<em class="replaceable">list</em></pre><p>Treats all list items as strings, but returns the"lowest string" as determined bythe <tt class="literal">lt</tt> operator. If the list is empty,<tt class="literal">minstr</tt> returns <tt class="literal">undef</tt>.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>reduce</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>reduce { <em class="replaceable">BLOCK</em> } @<em class="replaceable">list</em></pre><p>Literally "reduces"<tt class="literal">@</tt><em class="replaceable"><tt>list</tt></em> by calling<em class="replaceable"><tt>BLOCK</tt></em> until there are no more items tooperate on in <tt class="literal">@</tt><em class="replaceable"><tt>list</tt></em>.<tt class="literal">reduce</tt> sets <tt class="literal">$a</tt> and<tt class="literal">$b</tt> for each operation in<em class="replaceable"><tt>BLOCK</tt></em> and returns the reduced list as ascalar. If <tt class="literal">@</tt><em class="replaceable"><tt>list</tt></em> is<tt class="literal">0</tt>, <em class="replaceable"><tt>BLOCK</tt></em> is notexecuted, and<tt class="literal">$</tt><em class="replaceable"><tt>list</tt></em><tt class="literal">[0]</tt>is returned. If @<em class="replaceable"><tt>list</tt></em> is empty, then<tt class="literal">reduce</tt> returns <tt class="literal">undef</tt>.</p><p>You can sum and concatenate a list using <tt class="literal">reduce</tt>like so:</p><blockquote><pre class="code">my $sum_of_list = reduce { $a + $b } @ll; # summy $concat_list = reduce { $a . $b } @ll; # concat</pre></blockquote></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>shuffle</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>shuffle @LIST</pre><p>Returns list items in random order.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>sum</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>sum @LIST</pre><p>Returns the sum of all items in the list. Note that<tt class="literal">sum</tt> deals only with numerical list items and willignore any other list items. For example:</p><blockquote><pre class="code">my @ll = qw(1 3 5 nate Person pizza man carl_everett dinosaur 6.54);my $sum_of_list = sum(@ll);print "$sum_of_list\n"; # Prints '15.54'</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="ch08_139.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_141.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.139. lib</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.141. locale</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><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 + -