CINXE.COM
List::Util - A selection of general-utility list subroutines - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>List::Util - A selection of general-utility list subroutines - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/List::Util"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.26.2</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/List::Util">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/List::Util">5.40.1</a> <a class="dropdown-item" href="/5.40.0/List::Util">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/List::Util">5.38.3</a> <a class="dropdown-item" href="/5.38.2/List::Util">5.38.2</a> <a class="dropdown-item" href="/5.38.1/List::Util">5.38.1</a> <a class="dropdown-item" href="/5.38.0/List::Util">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/List::Util">5.36.3</a> <a class="dropdown-item" href="/5.36.2/List::Util">5.36.2</a> <a class="dropdown-item" href="/5.36.1/List::Util">5.36.1</a> <a class="dropdown-item" href="/5.36.0/List::Util">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/List::Util">5.34.3</a> <a class="dropdown-item" href="/5.34.2/List::Util">5.34.2</a> <a class="dropdown-item" href="/5.34.1/List::Util">5.34.1</a> <a class="dropdown-item" href="/5.34.0/List::Util">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/List::Util">5.32.1</a> <a class="dropdown-item" href="/5.32.0/List::Util">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/List::Util">5.30.3</a> <a class="dropdown-item" href="/5.30.2/List::Util">5.30.2</a> <a class="dropdown-item" href="/5.30.1/List::Util">5.30.1</a> <a class="dropdown-item" href="/5.30.0/List::Util">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/List::Util">5.28.3</a> <a class="dropdown-item" href="/5.28.2/List::Util">5.28.2</a> <a class="dropdown-item" href="/5.28.1/List::Util">5.28.1</a> <a class="dropdown-item" href="/5.28.0/List::Util">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/List::Util">5.26.3</a> <a class="dropdown-item active" href="/5.26.2/List::Util">5.26.2</a> <a class="dropdown-item" href="/5.26.1/List::Util">5.26.1</a> <a class="dropdown-item" href="/5.26.0/List::Util">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/List::Util">5.24.4</a> <a class="dropdown-item" href="/5.24.3/List::Util">5.24.3</a> <a class="dropdown-item" href="/5.24.2/List::Util">5.24.2</a> <a class="dropdown-item" href="/5.24.1/List::Util">5.24.1</a> <a class="dropdown-item" href="/5.24.0/List::Util">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/List::Util">5.22.4</a> <a class="dropdown-item" href="/5.22.3/List::Util">5.22.3</a> <a class="dropdown-item" href="/5.22.2/List::Util">5.22.2</a> <a class="dropdown-item" href="/5.22.1/List::Util">5.22.1</a> <a class="dropdown-item" href="/5.22.0/List::Util">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/List::Util">5.20.3</a> <a class="dropdown-item" href="/5.20.2/List::Util">5.20.2</a> <a class="dropdown-item" href="/5.20.1/List::Util">5.20.1</a> <a class="dropdown-item" href="/5.20.0/List::Util">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/List::Util">5.18.4</a> <a class="dropdown-item" href="/5.18.3/List::Util">5.18.3</a> <a class="dropdown-item" href="/5.18.2/List::Util">5.18.2</a> <a class="dropdown-item" href="/5.18.1/List::Util">5.18.1</a> <a class="dropdown-item" href="/5.18.0/List::Util">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/List::Util">5.16.3</a> <a class="dropdown-item" href="/5.16.2/List::Util">5.16.2</a> <a class="dropdown-item" href="/5.16.1/List::Util">5.16.1</a> <a class="dropdown-item" href="/5.16.0/List::Util">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/List::Util">5.14.4</a> <a class="dropdown-item" href="/5.14.3/List::Util">5.14.3</a> <a class="dropdown-item" href="/5.14.2/List::Util">5.14.2</a> <a class="dropdown-item" href="/5.14.1/List::Util">5.14.1</a> <a class="dropdown-item" href="/5.14.0/List::Util">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/List::Util">5.12.5</a> <a class="dropdown-item" href="/5.12.4/List::Util">5.12.4</a> <a class="dropdown-item" href="/5.12.3/List::Util">5.12.3</a> <a class="dropdown-item" href="/5.12.2/List::Util">5.12.2</a> <a class="dropdown-item" href="/5.12.1/List::Util">5.12.1</a> <a class="dropdown-item" href="/5.12.0/List::Util">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/List::Util">5.10.1</a> <a class="dropdown-item" href="/5.10.0/List::Util">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/List::Util">5.8.9</a> <a class="dropdown-item" href="/5.8.8/List::Util">5.8.8</a> <a class="dropdown-item" href="/5.8.7/List::Util">5.8.7</a> <a class="dropdown-item" href="/5.8.6/List::Util">5.8.6</a> <a class="dropdown-item" href="/5.8.5/List::Util">5.8.5</a> <a class="dropdown-item" href="/5.8.4/List::Util">5.8.4</a> <a class="dropdown-item" href="/5.8.3/List::Util">5.8.3</a> <a class="dropdown-item" href="/5.8.2/List::Util">5.8.2</a> <a class="dropdown-item" href="/5.8.1/List::Util">5.8.1</a> <a class="dropdown-item" href="/5.8.0/List::Util">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/List::Util">5.6.2</a> <a class="dropdown-item" href="/5.6.1/List::Util">5.6.1</a> <a class="dropdown-item" href="/5.6.0/List::Util">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/List::Util">5.005_04</a> <a class="dropdown-item" href="/5.005_03/List::Util">5.005_03</a> <a class="dropdown-item" href="/5.005_02/List::Util">5.005_02</a> <a class="dropdown-item" href="/5.005_01/List::Util">5.005_01</a> <a class="dropdown-item" href="/5.005/List::Util">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/List::Util">blead</a> <a class="dropdown-item" href="/5.41.10/List::Util">5.41.10</a> <a class="dropdown-item" href="/5.41.9/List::Util">5.41.9</a> <a class="dropdown-item" href="/5.41.8/List::Util">5.41.8</a> <a class="dropdown-item" href="/5.41.7/List::Util">5.41.7</a> <a class="dropdown-item" href="/5.41.6/List::Util">5.41.6</a> <a class="dropdown-item" href="/5.41.5/List::Util">5.41.5</a> <a class="dropdown-item" href="/5.41.4/List::Util">5.41.4</a> <a class="dropdown-item" href="/5.41.3/List::Util">5.41.3</a> <a class="dropdown-item" href="/5.41.2/List::Util">5.41.2</a> <a class="dropdown-item" href="/5.41.1/List::Util">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/List::Util">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/List::Util">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/List::Util">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/List::Util">5.39.10</a> <a class="dropdown-item" href="/5.39.9/List::Util">5.39.9</a> <a class="dropdown-item" href="/5.39.8/List::Util">5.39.8</a> <a class="dropdown-item" href="/5.39.7/List::Util">5.39.7</a> <a class="dropdown-item" href="/5.39.6/List::Util">5.39.6</a> <a class="dropdown-item" href="/5.39.5/List::Util">5.39.5</a> <a class="dropdown-item" href="/5.39.4/List::Util">5.39.4</a> <a class="dropdown-item" href="/5.39.3/List::Util">5.39.3</a> <a class="dropdown-item" href="/5.39.2/List::Util">5.39.2</a> <a class="dropdown-item" href="/5.39.1/List::Util">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/List::Util">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.26.2/perl">Perl</a> <a class="dropdown-item" href="/5.26.2/perlintro">Intro</a> <a class="dropdown-item" href="/5.26.2/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.26.2/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.26.2/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.2/perlop">Operators</a> <a class="dropdown-item" href="/5.26.2/functions">Functions</a> <a class="dropdown-item" href="/5.26.2/variables">Variables</a> <a class="dropdown-item" href="/5.26.2/modules">Modules</a> <a class="dropdown-item" href="/5.26.2/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.2/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.26.2/perlcommunity">Community</a> <a class="dropdown-item" href="/5.26.2/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.26.2/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.26.2/List::Util">List::Util</a> <div id="more"> (<a href="/5.26.2/List::Util.txt">source</a>, <a href="https://metacpan.org/pod/List::Util">CPAN</a>) </div> <div id="moduleversion">version 1.46_02</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.26.2. <a href="/List::Util">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#LIST-REDUCTION-FUNCTIONS">LIST-REDUCTION FUNCTIONS</a> <ul> <li> <a class="text-decoration-none" href="#reduce">reduce</a> </li> <li> <a class="text-decoration-none" href="#any">any</a> </li> <li> <a class="text-decoration-none" href="#all">all</a> </li> <li> <a class="text-decoration-none" href="#none">none</a> </li> <li> <a class="text-decoration-none" href="#notall">notall</a> </li> <li> <a class="text-decoration-none" href="#first">first</a> </li> <li> <a class="text-decoration-none" href="#max">max</a> </li> <li> <a class="text-decoration-none" href="#maxstr">maxstr</a> </li> <li> <a class="text-decoration-none" href="#min">min</a> </li> <li> <a class="text-decoration-none" href="#minstr">minstr</a> </li> <li> <a class="text-decoration-none" href="#product">product</a> </li> <li> <a class="text-decoration-none" href="#sum">sum</a> </li> <li> <a class="text-decoration-none" href="#sum0">sum0</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#KEY/VALUE-PAIR-LIST-FUNCTIONS">KEY/VALUE PAIR LIST FUNCTIONS</a> <ul> <li> <a class="text-decoration-none" href="#pairs">pairs</a> </li> <li> <a class="text-decoration-none" href="#unpairs">unpairs</a> </li> <li> <a class="text-decoration-none" href="#pairkeys">pairkeys</a> </li> <li> <a class="text-decoration-none" href="#pairvalues">pairvalues</a> </li> <li> <a class="text-decoration-none" href="#pairgrep">pairgrep</a> </li> <li> <a class="text-decoration-none" href="#pairfirst">pairfirst</a> </li> <li> <a class="text-decoration-none" href="#pairmap">pairmap</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#OTHER-FUNCTIONS">OTHER FUNCTIONS</a> <ul> <li> <a class="text-decoration-none" href="#shuffle">shuffle</a> </li> <li> <a class="text-decoration-none" href="#uniq">uniq</a> </li> <li> <a class="text-decoration-none" href="#uniqnum">uniqnum</a> </li> <li> <a class="text-decoration-none" href="#uniqstr">uniqstr</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#KNOWN-BUGS">KNOWN BUGS</a> <ul> <li> <a class="text-decoration-none" href="#RT-%2395409">RT #95409</a> </li> <li> <a class="text-decoration-none" href="#uniqnum()-on-oversized-bignums">uniqnum() on oversized bignums</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#SUGGESTED-ADDITIONS">SUGGESTED ADDITIONS</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#COPYRIGHT">COPYRIGHT</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>List::Util - A selection of general-utility list subroutines</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use List::Util qw( reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr );</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</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.</p> <h1 id="LIST-REDUCTION-FUNCTIONS"><a class="permalink" href="#LIST-REDUCTION-FUNCTIONS">#</a><a id="LIST"></a>LIST-REDUCTION FUNCTIONS</h1> <p>The following set of functions all reduce a list down to a single value.</p> <h2 id="reduce"><a class="permalink" href="#reduce">#</a>reduce</h2> <pre><code>$result = reduce { BLOCK } @list</code></pre> <p>Reduces <code>@list</code> by calling <code>BLOCK</code> in a scalar context multiple times, setting <code>$a</code> and <code>$b</code> each time. The first call will be with <code>$a</code> and <code>$b</code> set to the first two elements of the list, subsequent calls will be done by setting <code>$a</code> to the result of the previous call and <code>$b</code> to the next element in the list.</p> <p>Returns the result of the last call to the <code>BLOCK</code>. If <code>@list</code> is empty then <code>undef</code> is returned. If <code>@list</code> only contains one element then that element is returned and <code>BLOCK</code> is not executed.</p> <p>The following examples all demonstrate how <code>reduce</code> could be used to implement the other list-reduction functions in this module. (They are not in fact implemented like this, but instead in a more efficient manner in individual C functions).</p> <pre><code>$foo = reduce { defined($a) ? $a : $code->(local $_ = $b) ? $b : undef } undef, @list # first $foo = reduce { $a > $b ? $a : $b } 1..10 # max $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' # maxstr $foo = reduce { $a < $b ? $a : $b } 1..10 # min $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr $foo = reduce { $a + $b } 1 .. 10 # sum $foo = reduce { $a . $b } @bar # concat $foo = reduce { $a || $code->(local $_ = $b) } 0, @bar # any $foo = reduce { $a && $code->(local $_ = $b) } 1, @bar # all $foo = reduce { $a && !$code->(local $_ = $b) } 1, @bar # none $foo = reduce { $a || !$code->(local $_ = $b) } 0, @bar # notall # Note that these implementations do not fully short-circuit</code></pre> <p>If your algorithm requires that <code>reduce</code> produce an identity value, then make sure that you always pass that identity value as the first argument to prevent <code>undef</code> being returned</p> <pre><code>$foo = reduce { $a + $b } 0, @values; # sum with 0 identity value</code></pre> <p>The above example code blocks also suggest how to use <code>reduce</code> to build a more efficient combined version of one of these basic functions and a <code>map</code> block. For example, to find the total length of the all the strings in a list, we could use</p> <pre><code>$total = sum map { length } @strings;</code></pre> <p>However, this produces a list of temporary integer values as long as the original list of strings, only to reduce it down to a single value again. We can compute the same result more efficiently by using <code>reduce</code> with a code block that accumulates lengths by writing this instead as:</p> <pre><code>$total = reduce { $a + length $b } 0, @strings</code></pre> <p>The remaining list-reduction functions are all specialisations of this generic idea.</p> <h2 id="any"><a class="permalink" href="#any">#</a>any</h2> <pre><code>my $bool = any { BLOCK } @list;</code></pre> <p><i>Since version 1.33.</i></p> <p>Similar to <code>grep</code> in that it evaluates <code>BLOCK</code> setting <code>$_</code> to each element of <code>@list</code> in turn. <code>any</code> returns true if any element makes the <code>BLOCK</code> return a true value. If <code>BLOCK</code> never returns true or <code>@list</code> was empty then it returns false.</p> <p>Many cases of using <code>grep</code> in a conditional can be written using <code>any</code> instead, as it can short-circuit after the first true result.</p> <pre><code>if( any { length > 10 } @strings ) { # at least one string has more than 10 characters }</code></pre> <h2 id="all"><a class="permalink" href="#all">#</a>all</h2> <pre><code>my $bool = all { BLOCK } @list;</code></pre> <p><i>Since version 1.33.</i></p> <p>Similar to <a href="#any">"any"</a>, except that it requires all elements of the <code>@list</code> to make the <code>BLOCK</code> return true. If any element returns false, then it returns false. If the <code>BLOCK</code> never returns false or the <code>@list</code> was empty then it returns true.</p> <h2 id="none"><a class="permalink" href="#none">#</a>none</h2> <h2 id="notall"><a class="permalink" href="#notall">#</a>notall</h2> <pre><code>my $bool = none { BLOCK } @list; my $bool = notall { BLOCK } @list;</code></pre> <p><i>Since version 1.33.</i></p> <p>Similar to <a href="#any">"any"</a> and <a href="#all">"all"</a>, but with the return sense inverted. <code>none</code> returns true only if no value in the <code>@list</code> causes the <code>BLOCK</code> to return true, and <code>notall</code> returns true only if not all of the values do.</p> <h2 id="first"><a class="permalink" href="#first">#</a>first</h2> <pre><code>my $val = first { BLOCK } @list;</code></pre> <p>Similar to <code>grep</code> in that it evaluates <code>BLOCK</code> setting <code>$_</code> to each element of <code>@list</code> in turn. <code>first</code> returns the first element where the result from <code>BLOCK</code> is a true value. If <code>BLOCK</code> never returns true or <code>@list</code> was empty then <code>undef</code> is returned.</p> <pre><code>$foo = first { defined($_) } @list # first defined value in @list $foo = first { $_ > $value } @list # first value in @list which # is greater than $value</code></pre> <h2 id="max"><a class="permalink" href="#max">#</a>max</h2> <pre><code>my $num = max @list;</code></pre> <p>Returns the entry in the list with the highest numerical value. If the list is empty then <code>undef</code> is returned.</p> <pre><code>$foo = max 1..10 # 10 $foo = max 3,9,12 # 12 $foo = max @bar, @baz # whatever</code></pre> <h2 id="maxstr"><a class="permalink" href="#maxstr">#</a>maxstr</h2> <pre><code>my $str = maxstr @list;</code></pre> <p>Similar to <a href="#max">"max"</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 <code>undef</code> is returned.</p> <pre><code>$foo = maxstr 'A'..'Z' # 'Z' $foo = maxstr "hello","world" # "world" $foo = maxstr @bar, @baz # whatever</code></pre> <h2 id="min"><a class="permalink" href="#min">#</a>min</h2> <pre><code>my $num = min @list;</code></pre> <p>Similar to <a href="#max">"max"</a> but returns the entry in the list with the lowest numerical value. If the list is empty then <code>undef</code> is returned.</p> <pre><code>$foo = min 1..10 # 1 $foo = min 3,9,12 # 3 $foo = min @bar, @baz # whatever</code></pre> <h2 id="minstr"><a class="permalink" href="#minstr">#</a>minstr</h2> <pre><code>my $str = minstr @list;</code></pre> <p>Similar to <a href="#min">"min"</a>, but treats all the entries in the list as strings and returns the lowest string as defined by the <code>lt</code> operator. If the list is empty then <code>undef</code> is returned.</p> <pre><code>$foo = minstr 'A'..'Z' # 'A' $foo = minstr "hello","world" # "hello" $foo = minstr @bar, @baz # whatever</code></pre> <h2 id="product"><a class="permalink" href="#product">#</a>product</h2> <pre><code>my $num = product @list;</code></pre> <p><i>Since version 1.35.</i></p> <p>Returns the numerical product of all the elements in <code>@list</code>. If <code>@list</code> is empty then <code>1</code> is returned.</p> <pre><code>$foo = product 1..10 # 3628800 $foo = product 3,9,12 # 324</code></pre> <h2 id="sum"><a class="permalink" href="#sum">#</a>sum</h2> <pre><code>my $num_or_undef = sum @list;</code></pre> <p>Returns the numerical sum of all the elements in <code>@list</code>. For backwards compatibility, if <code>@list</code> is empty then <code>undef</code> is returned.</p> <pre><code>$foo = sum 1..10 # 55 $foo = sum 3,9,12 # 24 $foo = sum @bar, @baz # whatever</code></pre> <h2 id="sum0"><a class="permalink" href="#sum0">#</a>sum0</h2> <pre><code>my $num = sum0 @list;</code></pre> <p><i>Since version 1.26.</i></p> <p>Similar to <a href="#sum">"sum"</a>, except this returns 0 when given an empty list, rather than <code>undef</code>.</p> <h1 id="KEY/VALUE-PAIR-LIST-FUNCTIONS"><a class="permalink" href="#KEY/VALUE-PAIR-LIST-FUNCTIONS">#</a><a id="KEY"></a><a id="KEY-VALUE-PAIR-LIST-FUNCTIONS"></a>KEY/VALUE PAIR LIST FUNCTIONS</h1> <p>The following set of functions, all inspired by <a href="/5.26.2/List::Pairwise">List::Pairwise</a>, consume an even-sized list of pairs. The pairs may be key/value associations from a hash, or just a list of values. The functions will all preserve the original ordering of the pairs, and will not be confused by multiple pairs having the same "key" value - nor even do they require that the first of each pair be a plain string.</p> <p><b>NOTE</b>: At the time of writing, the following <code>pair*</code> functions that take a block do not modify the value of <code>$_</code> within the block, and instead operate using the <code>$a</code> and <code>$b</code> globals instead. This has turned out to be a poor design, as it precludes the ability to provide a <code>pairsort</code> function. Better would be to pass pair-like objects as 2-element array references in <code>$_</code>, in a style similar to the return value of the <code>pairs</code> function. At some future version this behaviour may be added.</p> <p>Until then, users are alerted <b>NOT</b> to rely on the value of <code>$_</code> remaining unmodified between the outside and the inside of the control block. In particular, the following example is <b>UNSAFE</b>:</p> <pre><code>my @kvlist = ... foreach (qw( some keys here )) { my @items = pairgrep { $a eq $_ } @kvlist; ... }</code></pre> <p>Instead, write this using a lexical variable:</p> <pre><code>foreach my $key (qw( some keys here )) { my @items = pairgrep { $a eq $key } @kvlist; ... }</code></pre> <h2 id="pairs"><a class="permalink" href="#pairs">#</a>pairs</h2> <pre><code>my @pairs = pairs @kvlist;</code></pre> <p><i>Since version 1.29.</i></p> <p>A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of <code>ARRAY</code> references, each containing two items from the given list. It is a more efficient version of</p> <pre><code>@pairs = pairmap { [ $a, $b ] } @kvlist</code></pre> <p>It is most convenient to use in a <code>foreach</code> loop, for example:</p> <pre><code>foreach my $pair ( pairs @kvlist ) { my ( $key, $value ) = @$pair; ... }</code></pre> <p>Since version <code>1.39</code> these <code>ARRAY</code> references are blessed objects, recognising the two methods <code>key</code> and <code>value</code>. The following code is equivalent:</p> <pre><code>foreach my $pair ( pairs @kvlist ) { my $key = $pair->key; my $value = $pair->value; ... }</code></pre> <h2 id="unpairs"><a class="permalink" href="#unpairs">#</a>unpairs</h2> <pre><code>my @kvlist = unpairs @pairs</code></pre> <p><i>Since version 1.42.</i></p> <p>The inverse function to <code>pairs</code>; this function takes a list of <code>ARRAY</code> references containing two elements each, and returns a flattened list of the two values from each of the pairs, in order. This is notionally equivalent to</p> <pre><code>my @kvlist = map { @{$_}[0,1] } @pairs</code></pre> <p>except that it is implemented more efficiently internally. Specifically, for any input item it will extract exactly two values for the output list; using <code>undef</code> if the input array references are short.</p> <p>Between <code>pairs</code> and <code>unpairs</code>, a higher-order list function can be used to operate on the pairs as single scalars; such as the following near-equivalents of the other <code>pair*</code> higher-order functions:</p> <pre><code>@kvlist = unpairs grep { FUNC } pairs @kvlist # Like pairgrep, but takes $_ instead of $a and $b @kvlist = unpairs map { FUNC } pairs @kvlist # Like pairmap, but takes $_ instead of $a and $b</code></pre> <p>Note however that these versions will not behave as nicely in scalar context.</p> <p>Finally, this technique can be used to implement a sort on a keyvalue pair list; e.g.:</p> <pre><code>@kvlist = unpairs sort { $a->key cmp $b->key } pairs @kvlist</code></pre> <h2 id="pairkeys"><a class="permalink" href="#pairkeys">#</a>pairkeys</h2> <pre><code>my @keys = pairkeys @kvlist;</code></pre> <p><i>Since version 1.29.</i></p> <p>A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of the the first values of each of the pairs in the given list. It is a more efficient version of</p> <pre><code>@keys = pairmap { $a } @kvlist</code></pre> <h2 id="pairvalues"><a class="permalink" href="#pairvalues">#</a>pairvalues</h2> <pre><code>my @values = pairvalues @kvlist;</code></pre> <p><i>Since version 1.29.</i></p> <p>A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of the the second values of each of the pairs in the given list. It is a more efficient version of</p> <pre><code>@values = pairmap { $b } @kvlist</code></pre> <h2 id="pairgrep"><a class="permalink" href="#pairgrep">#</a>pairgrep</h2> <pre><code>my @kvlist = pairgrep { BLOCK } @kvlist; my $count = pairgrep { BLOCK } @kvlist;</code></pre> <p><i>Since version 1.29.</i></p> <p>Similar to perl's <code>grep</code> keyword, but interprets the given list as an even-sized list of pairs. It invokes the <code>BLOCK</code> multiple times, in scalar context, with <code>$a</code> and <code>$b</code> set to successive pairs of values from the <code>@kvlist</code>.</p> <p>Returns an even-sized list of those pairs for which the <code>BLOCK</code> returned true in list context, or the count of the <b>number of pairs</b> in scalar context. (Note, therefore, in scalar context that it returns a number half the size of the count of items it would have returned in list context).</p> <pre><code>@subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist</code></pre> <p>As with <code>grep</code> aliasing <code>$_</code> to list elements, <code>pairgrep</code> aliases <code>$a</code> and <code>$b</code> to elements of the given list. Any modifications of it by the code block will be visible to the caller.</p> <h2 id="pairfirst"><a class="permalink" href="#pairfirst">#</a>pairfirst</h2> <pre><code>my ( $key, $val ) = pairfirst { BLOCK } @kvlist; my $found = pairfirst { BLOCK } @kvlist;</code></pre> <p><i>Since version 1.30.</i></p> <p>Similar to the <a href="#first">"first"</a> function, but interprets the given list as an even-sized list of pairs. It invokes the <code>BLOCK</code> multiple times, in scalar context, with <code>$a</code> and <code>$b</code> set to successive pairs of values from the <code>@kvlist</code>.</p> <p>Returns the first pair of values from the list for which the <code>BLOCK</code> returned true in list context, or an empty list of no such pair was found. In scalar context it returns a simple boolean value, rather than either the key or the value found.</p> <pre><code>( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist</code></pre> <p>As with <code>grep</code> aliasing <code>$_</code> to list elements, <code>pairfirst</code> aliases <code>$a</code> and <code>$b</code> to elements of the given list. Any modifications of it by the code block will be visible to the caller.</p> <h2 id="pairmap"><a class="permalink" href="#pairmap">#</a>pairmap</h2> <pre><code>my @list = pairmap { BLOCK } @kvlist; my $count = pairmap { BLOCK } @kvlist;</code></pre> <p><i>Since version 1.29.</i></p> <p>Similar to perl's <code>map</code> keyword, but interprets the given list as an even-sized list of pairs. It invokes the <code>BLOCK</code> multiple times, in list context, with <code>$a</code> and <code>$b</code> set to successive pairs of values from the <code>@kvlist</code>.</p> <p>Returns the concatenation of all the values returned by the <code>BLOCK</code> in list context, or the count of the number of items that would have been returned in scalar context.</p> <pre><code>@result = pairmap { "The key $a has value $b" } @kvlist</code></pre> <p>As with <code>map</code> aliasing <code>$_</code> to list elements, <code>pairmap</code> aliases <code>$a</code> and <code>$b</code> to elements of the given list. Any modifications of it by the code block will be visible to the caller.</p> <p>See <a href="#KNOWN-BUGS">"KNOWN BUGS"</a> for a known-bug with <code>pairmap</code>, and a workaround.</p> <h1 id="OTHER-FUNCTIONS"><a class="permalink" href="#OTHER-FUNCTIONS">#</a><a id="OTHER"></a>OTHER FUNCTIONS</h1> <h2 id="shuffle"><a class="permalink" href="#shuffle">#</a>shuffle</h2> <pre><code>my @values = shuffle @values;</code></pre> <p>Returns the values of the input in a random order</p> <pre><code>@cards = shuffle 0..51 # 0..51 in a random order</code></pre> <h2 id="uniq"><a class="permalink" href="#uniq">#</a>uniq</h2> <pre><code>my @subset = uniq @values</code></pre> <p><i>Since version 1.45.</i></p> <p>Filters a list of values to remove subsequent duplicates, as judged by a DWIM-ish string equality or <code>undef</code> test. Preserves the order of unique elements, and retains the first value of any duplicate set.</p> <pre><code>my $count = uniq @values</code></pre> <p>In scalar context, returns the number of elements that would have been returned as a list.</p> <p>The <code>undef</code> value is treated by this function as distinct from the empty string, and no warning will be produced. It is left as-is in the returned list. Subsequent <code>undef</code> values are still considered identical to the first, and will be removed.</p> <h2 id="uniqnum"><a class="permalink" href="#uniqnum">#</a>uniqnum</h2> <pre><code>my @subset = uniqnum @values</code></pre> <p><i>Since version 1.44.</i></p> <p>Filters a list of values to remove subsequent duplicates, as judged by a numerical equality test. Preserves the order of unique elements, and retains the first value of any duplicate set.</p> <pre><code>my $count = uniqnum @values</code></pre> <p>In scalar context, returns the number of elements that would have been returned as a list.</p> <p>Note that <code>undef</code> is treated much as other numerical operations treat it; it compares equal to zero but additionally produces a warning if such warnings are enabled (<code>use warnings 'uninitialized';</code>). In addition, an <code>undef</code> in the returned list is coerced into a numerical zero, so that the entire list of values returned by <code>uniqnum</code> are well-behaved as numbers.</p> <p>Note also that multiple IEEE <code>NaN</code> values are treated as duplicates of each other, regardless of any differences in their payloads, and despite the fact that <code>0+'NaN' == 0+'NaN'</code> yields false.</p> <h2 id="uniqstr"><a class="permalink" href="#uniqstr">#</a>uniqstr</h2> <pre><code>my @subset = uniqstr @values</code></pre> <p><i>Since version 1.45.</i></p> <p>Filters a list of values to remove subsequent duplicates, as judged by a string equality test. Preserves the order of unique elements, and retains the first value of any duplicate set.</p> <pre><code>my $count = uniqstr @values</code></pre> <p>In scalar context, returns the number of elements that would have been returned as a list.</p> <p>Note that <code>undef</code> is treated much as other string operations treat it; it compares equal to the empty string but additionally produces a warning if such warnings are enabled (<code>use warnings 'uninitialized';</code>). In addition, an <code>undef</code> in the returned list is coerced into an empty string, so that the entire list of values returned by <code>uniqstr</code> are well-behaved as strings.</p> <h1 id="KNOWN-BUGS"><a class="permalink" href="#KNOWN-BUGS">#</a><a id="KNOWN"></a>KNOWN BUGS</h1> <h2 id="RT-#95409"><a class="permalink" href="#RT-%2395409">#</a><a id="RT"></a><a id="RT-95409"></a>RT #95409</h2> <p><a href="https://rt.cpan.org/Ticket/Display.html?id=95409">https://rt.cpan.org/Ticket/Display.html?id=95409</a></p> <p>If the block of code given to <a href="#pairmap">"pairmap"</a> contains lexical variables that are captured by a returned closure, and the closure is executed after the block has been re-used for the next iteration, these lexicals will not see the correct values. For example:</p> <pre><code>my @subs = pairmap { my $var = "$a is $b"; sub { print "$var\n" }; } one => 1, two => 2, three => 3; $_->() for @subs;</code></pre> <p>Will incorrectly print</p> <pre><code class="plaintext">three is 3 three is 3 three is 3</code></pre> <p>This is due to the performance optimisation of using <code>MULTICALL</code> for the code block, which means that fresh SVs do not get allocated for each call to the block. Instead, the same SV is re-assigned for each iteration, and all the closures will share the value seen on the final iteration.</p> <p>To work around this bug, surround the code with a second set of braces. This creates an inner block that defeats the <code>MULTICALL</code> logic, and does get fresh SVs allocated each time:</p> <pre><code>my @subs = pairmap { { my $var = "$a is $b"; sub { print "$var\n"; } } } one => 1, two => 2, three => 3;</code></pre> <p>This bug only affects closures that are generated by the block but used afterwards. Lexical variables that are only used during the lifetime of the block's execution will take their individual values for each invocation, as normal.</p> <h2 id="uniqnum()-on-oversized-bignums"><a class="permalink" href="#uniqnum()-on-oversized-bignums">#</a><a id="uniqnum1"></a><a id="uniqnum-on-oversized-bignums"></a>uniqnum() on oversized bignums</h2> <p>Due to the way that <code>uniqnum()</code> compares numbers, it cannot distinguish differences between bignums (especially bigints) that are too large to fit in the native platform types. For example,</p> <pre><code>my $x = Math::BigInt->new( "1" x 100 ); my $y = $x + 1; say for uniqnum( $x, $y );</code></pre> <p>Will print just the value of <code>$x</code>, believing that <code>$y</code> is a numerically- equivalent value. This bug does not affect <code>uniqstr()</code>, which will correctly observe that the two values stringify to different strings.</p> <h1 id="SUGGESTED-ADDITIONS"><a class="permalink" href="#SUGGESTED-ADDITIONS">#</a><a id="SUGGESTED"></a>SUGGESTED ADDITIONS</h1> <p>The following are additions that have been requested, but I have been reluctant to add due to them being very simple to implement in perl</p> <pre><code># How many elements are true sub true { scalar grep { $_ } @_ } # How many elements are false sub false { scalar grep { !$_ } @_ }</code></pre> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.26.2/Scalar::Util">Scalar::Util</a>, <a href="/5.26.2/List::MoreUtils">List::MoreUtils</a></p> <h1 id="COPYRIGHT"><a class="permalink" href="#COPYRIGHT">#</a>COPYRIGHT</h1> <p>Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p> <p>Recent additions and current maintenance by Paul Evans, <leonerd@leonerd.org.uk>.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>