util.html
来自「perl教程」· HTML 代码 · 共 287 行 · 第 1/2 页
HTML
287 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>List::Util - A selection of general-utility list subroutines</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>List::Util - A selection of general-utility list subroutines</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#known_bugs">KNOWN BUGS</a></li>
<li><a href="#suggested_additions">SUGGESTED ADDITIONS</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>List::Util - A selection of general-utility list subroutines</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">List::Util</span> <span class="string">qw(first max maxstr min minstr reduce shuffle sum)</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><code>List::Util</code> contains a selection of subroutines that people have
expressed would be nice to have in the perl core, but the usage would
not really be high enough to warrant the use of a keyword, and the size
so small such that being individual extensions would be wasteful.</p>
<p>By default <code>List::Util</code> does not export any subroutines. The
subroutines defined are</p>
<dl>
<dt><strong><a name="item_first">first BLOCK LIST</a></strong>
<dd>
<p>Similar to <a href="../../lib/Pod/perlfunc.html#item_grep"><code>grep</code></a> in that it evaluates BLOCK setting <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> to each element
of LIST in turn. <a href="#item_first"><code>first</code></a> returns the first element where the result from
BLOCK is a true value. If BLOCK never returns true or LIST was empty then
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is returned.</p>
</dd>
<dd>
<pre>
<span class="variable">$foo</span> <span class="operator">=</span> <span class="variable">first</span> <span class="operator">{</span> <span class="keyword">defined</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)</span> <span class="operator">}</span> <span class="variable">@list</span> <span class="comment"># first defined value in @list</span>
<span class="variable">$foo</span> <span class="operator">=</span> <span class="variable">first</span> <span class="operator">{</span> <span class="variable">$_</span> <span class="operator">></span> <span class="variable">$value</span> <span class="operator">}</span> <span class="variable">@list</span> <span class="comment"># first value in @list which</span>
<span class="comment"># is greater than $value</span>
</pre>
</dd>
<dd>
<p>This function could be implemented using <a href="#item_reduce"><code>reduce</code></a> like this</p>
</dd>
<dd>
<pre>
<span class="variable">$foo</span> <span class="operator">=</span> <span class="variable">reduce</span> <span class="operator">{</span> <span class="keyword">defined</span><span class="operator">(</span><span class="variable">$a</span><span class="operator">)</span> <span class="operator">?</span> <span class="variable">$a</span> <span class="operator">:</span> <span class="variable">wanted</span><span class="operator">(</span><span class="variable">$b</span><span class="operator">)</span> <span class="operator">?</span> <span class="variable">$b</span> <span class="operator">:</span> <span class="keyword">undef</span> <span class="operator">}</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="variable">@list</span>
</pre>
</dd>
<dd>
<p>for example <code>wanted()</code> could be <a href="../../lib/Pod/perlfunc.html#item_defined"><code>defined()</code></a> which would return the first
defined value in @list</p>
</dd>
</li>
<dt><strong><a name="item_max">max LIST</a></strong>
<dd>
<p>Returns the entry in the list with the highest numerical value. If the
list is empty then <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is returned.</p>
</dd>
<dd>
<pre>
$foo = max 1..10 # 10
$foo = max 3,9,12 # 12
$foo = max @bar, @baz # whatever</pre>
</dd>
<dd>
<p>This function could be implemented using <a href="#item_reduce"><code>reduce</code></a> like this</p>
</dd>
<dd>
<pre>
<span class="variable">$foo</span> <span class="operator">=</span> <span class="variable">reduce</span> <span class="operator">{</span> <span class="variable">$a</span> <span class="operator">></span> <span class="variable">$b</span> <span class="operator">?</span> <span class="variable">$a</span> <span class="operator">:</span> <span class="variable">$b</span> <span class="operator">}</span> <span class="number">1</span><span class="operator">..</span><span class="number">10</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_maxstr">maxstr LIST</a></strong>
<dd>
<p>Similar to <a href="#item_max"><code>max</code></a>, but treats all the entries in the list as strings
and returns the highest string as defined by the <code>gt</code> operator.
If the list is empty then <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is returned.</p>
</dd>
<dd>
<pre>
$foo = maxstr 'A'..'Z' # 'Z'
$foo = maxstr "hello","world" # "world"
$foo = maxstr @bar, @baz # whatever</pre>
</dd>
<dd>
<p>This function could be implemented using <a href="#item_reduce"><code>reduce</code></a> like this</p>
</dd>
<dd>
<pre>
<span class="variable">$foo</span> <span class="operator">=</span> <span class="variable">reduce</span> <span class="operator">{</span> <span class="variable">$a</span> <span class="keyword">gt</span> <span class="variable">$b</span> <span class="operator">?</span> <span class="variable">$a</span> <span class="operator">:</span> <span class="variable">$b</span> <span class="operator">}</span> <span class="string">'A'</span><span class="operator">..</span><span class="string">'Z'</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_min">min LIST</a></strong>
<dd>
<p>Similar to <a href="#item_max"><code>max</code></a> but returns the entry in the list with the lowest
numerical value. If the list is empty then <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is returned.</p>
</dd>
<dd>
<pre>
$foo = min 1..10 # 1
$foo = min 3,9,12 # 3
$foo = min @bar, @baz # whatever</pre>
</dd>
<dd>
<p>This function could be implemented using <a href="#item_reduce"><code>reduce</code></a> like this</p>
</dd>
<dd>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?