.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "List::Util 3perl" .TH List::Util 3perl "2014-12-27" "perl v5.20.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" List::Util \- A selection of general\-utility list subroutines .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use List::Util qw(first max maxstr min minstr reduce shuffle sum); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`List::Util\*(C'\fR 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. .PP By default \f(CW\*(C`List::Util\*(C'\fR does not export any subroutines. .SH "LIST-REDUCTION FUNCTIONS" .IX Header "LIST-REDUCTION FUNCTIONS" The following set of functions all reduce a list down to a single value. .ie n .SS "$result = reduce { \s-1BLOCK \s0} @list" .el .SS "\f(CW$result\fP = reduce { \s-1BLOCK \s0} \f(CW@list\fP" .IX Subsection "$result = reduce { BLOCK } @list" Reduces \f(CW@list\fR by calling \f(CW\*(C`BLOCK\*(C'\fR in a scalar context multiple times, setting \f(CW$a\fR and \f(CW$b\fR each time. The first call will be with \f(CW$a\fR and \f(CW$b\fR set to the first two elements of the list, subsequent calls will be done by setting \f(CW$a\fR to the result of the previous call and \f(CW$b\fR to the next element in the list. .PP Returns the result of the last call to the \f(CW\*(C`BLOCK\*(C'\fR. If \f(CW@list\fR is empty then \&\f(CW\*(C`undef\*(C'\fR is returned. If \f(CW@list\fR only contains one element then that element is returned and \f(CW\*(C`BLOCK\*(C'\fR is not executed. .PP The following examples all demonstrate how \f(CW\*(C`reduce\*(C'\fR 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). .PP .Vb 3 \& $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 } \*(AqA\*(Aq..\*(AqZ\*(Aq # maxstr \& $foo = reduce { $a < $b ? $a : $b } 1..10 # min \& $foo = reduce { $a lt $b ? $a : $b } \*(Aqaa\*(Aq..\*(Aqzz\*(Aq # 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 .Ve .PP If your algorithm requires that \f(CW\*(C`reduce\*(C'\fR produce an identity value, then make sure that you always pass that identity value as the first argument to prevent \&\f(CW\*(C`undef\*(C'\fR being returned .PP .Vb 1 \& $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value .Ve .PP The remaining list-reduction functions are all specialisations of this generic idea. .ie n .SS "$b = any { \s-1BLOCK \s0} @list" .el .SS "\f(CW$b\fP = any { \s-1BLOCK \s0} \f(CW@list\fP" .IX Subsection "$b = any { BLOCK } @list" Similar to \f(CW\*(C`grep\*(C'\fR in that it evaluates \f(CW\*(C`BLOCK\*(C'\fR setting \f(CW$_\fR to each element of \f(CW@list\fR in turn. \f(CW\*(C`any\*(C'\fR returns true if any element makes the \f(CW\*(C`BLOCK\*(C'\fR return a true value. If \f(CW\*(C`BLOCK\*(C'\fR never returns true or \f(CW@list\fR was empty then it returns false. .PP Many cases of using \f(CW\*(C`grep\*(C'\fR in a conditional can be written using \f(CW\*(C`any\*(C'\fR instead, as it can short-circuit after the first true result. .PP .Vb 3 \& if( any { length > 10 } @strings ) { \& # at least one string has more than 10 characters \& } .Ve .ie n .SS "$b = all { \s-1BLOCK \s0} @list" .el .SS "\f(CW$b\fP = all { \s-1BLOCK \s0} \f(CW@list\fP" .IX Subsection "$b = all { BLOCK } @list" Similar to \f(CW\*(C`any\*(C'\fR, except that it requires all elements of the \f(CW@list\fR to make the \f(CW\*(C`BLOCK\*(C'\fR return true. If any element returns false, then it returns false. If the \f(CW\*(C`BLOCK\*(C'\fR never returns false or the \f(CW@list\fR was empty then it returns true. .ie n .SS "$b = none { \s-1BLOCK \s0} @list" .el .SS "\f(CW$b\fP = none { \s-1BLOCK \s0} \f(CW@list\fP" .IX Subsection "$b = none { BLOCK } @list" .ie n .SS "$b = notall { \s-1BLOCK \s0} @list" .el .SS "\f(CW$b\fP = notall { \s-1BLOCK \s0} \f(CW@list\fP" .IX Subsection "$b = notall { BLOCK } @list" Similar to \f(CW\*(C`any\*(C'\fR and \f(CW\*(C`all\*(C'\fR, but with the return sense inverted. \f(CW\*(C`none\*(C'\fR returns true only if no value in the \s-1LIST\s0 causes the \s-1BLOCK\s0 to return true, and \&\f(CW\*(C`notall\*(C'\fR returns true only if not all of the values do. .ie n .SS "$val = first { \s-1BLOCK \s0} @list" .el .SS "\f(CW$val\fP = first { \s-1BLOCK \s0} \f(CW@list\fP" .IX Subsection "$val = first { BLOCK } @list" Similar to \f(CW\*(C`grep\*(C'\fR in that it evaluates \f(CW\*(C`BLOCK\*(C'\fR setting \f(CW$_\fR to each element of \f(CW@list\fR in turn. \f(CW\*(C`first\*(C'\fR returns the first element where the result from \&\f(CW\*(C`BLOCK\*(C'\fR is a true value. If \f(CW\*(C`BLOCK\*(C'\fR never returns true or \f(CW@list\fR was empty then \f(CW\*(C`undef\*(C'\fR is returned. .PP .Vb 3 \& $foo = first { defined($_) } @list # first defined value in @list \& $foo = first { $_ > $value } @list # first value in @list which \& # is greater than $value .Ve .ie n .SS "$num = max @list" .el .SS "\f(CW$num\fP = max \f(CW@list\fP" .IX Subsection "$num = max @list" Returns the entry in the list with the highest numerical value. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. .PP .Vb 3 \& $foo = max 1..10 # 10 \& $foo = max 3,9,12 # 12 \& $foo = max @bar, @baz # whatever .Ve .ie n .SS "$str = maxstr @list" .el .SS "\f(CW$str\fP = maxstr \f(CW@list\fP" .IX Subsection "$str = maxstr @list" Similar to \f(CW\*(C`max\*(C'\fR, but treats all the entries in the list as strings and returns the highest string as defined by the \f(CW\*(C`gt\*(C'\fR operator. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. .PP .Vb 3 \& $foo = maxstr \*(AqA\*(Aq..\*(AqZ\*(Aq # \*(AqZ\*(Aq \& $foo = maxstr "hello","world" # "world" \& $foo = maxstr @bar, @baz # whatever .Ve .ie n .SS "$num = min @list" .el .SS "\f(CW$num\fP = min \f(CW@list\fP" .IX Subsection "$num = min @list" Similar to \f(CW\*(C`max\*(C'\fR but returns the entry in the list with the lowest numerical value. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. .PP .Vb 3 \& $foo = min 1..10 # 1 \& $foo = min 3,9,12 # 3 \& $foo = min @bar, @baz # whatever .Ve .ie n .SS "$str = minstr @list" .el .SS "\f(CW$str\fP = minstr \f(CW@list\fP" .IX Subsection "$str = minstr @list" Similar to \f(CW\*(C`min\*(C'\fR, but treats all the entries in the list as strings and returns the lowest string as defined by the \f(CW\*(C`lt\*(C'\fR operator. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. .PP .Vb 3 \& $foo = minstr \*(AqA\*(Aq..\*(AqZ\*(Aq # \*(AqA\*(Aq \& $foo = minstr "hello","world" # "hello" \& $foo = minstr @bar, @baz # whatever .Ve .ie n .SS "$num = product @list" .el .SS "\f(CW$num\fP = product \f(CW@list\fP" .IX Subsection "$num = product @list" Returns the numerical product of all the elements in \f(CW@list\fR. If \f(CW@list\fR is empty then \f(CW1\fR is returned. .PP .Vb 2 \& $foo = product 1..10 # 3628800 \& $foo = product 3,9,12 # 324 .Ve .ie n .SS "$num_or_undef = sum @list" .el .SS "\f(CW$num_or_undef\fP = sum \f(CW@list\fP" .IX Subsection "$num_or_undef = sum @list" Returns the numerical sum of all the elements in \f(CW@list\fR. For backwards compatibility, if \f(CW@list\fR is empty then \f(CW\*(C`undef\*(C'\fR is returned. .PP .Vb 3 \& $foo = sum 1..10 # 55 \& $foo = sum 3,9,12 # 24 \& $foo = sum @bar, @baz # whatever .Ve .ie n .SS "$num = sum0 @list" .el .SS "\f(CW$num\fP = sum0 \f(CW@list\fP" .IX Subsection "$num = sum0 @list" Similar to \f(CW\*(C`sum\*(C'\fR, except this returns 0 when given an empty list, rather than \&\f(CW\*(C`undef\*(C'\fR. .SH "KEY/VALUE PAIR LIST FUNCTIONS" .IX Header "KEY/VALUE PAIR LIST FUNCTIONS" The following set of functions, all inspired by List::Pairwise, 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 \*(L"key\*(R" value \- nor even do they require that the first of each pair be a plain string. .ie n .SS "@kvlist = pairgrep { \s-1BLOCK \s0} @kvlist" .el .SS "\f(CW@kvlist\fP = pairgrep { \s-1BLOCK \s0} \f(CW@kvlist\fP" .IX Subsection "@kvlist = pairgrep { BLOCK } @kvlist" .ie n .SS "$count = pairgrep { \s-1BLOCK \s0} @kvlist" .el .SS "\f(CW$count\fP = pairgrep { \s-1BLOCK \s0} \f(CW@kvlist\fP" .IX Subsection "$count = pairgrep { BLOCK } @kvlist" Similar to perl's \f(CW\*(C`grep\*(C'\fR keyword, but interprets the given list as an even-sized list of pairs. It invokes the \f(CW\*(C`BLOCK\*(C'\fR multiple times, in scalar context, with \f(CW$a\fR and \f(CW$b\fR set to successive pairs of values from the \&\f(CW@kvlist\fR. .PP Returns an even-sized list of those pairs for which the \f(CW\*(C`BLOCK\*(C'\fR returned true in list context, or the count of the \fBnumber of pairs\fR 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). .PP .Vb 1 \& @subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist .Ve .PP As with \f(CW\*(C`grep\*(C'\fR aliasing \f(CW$_\fR to list elements, \f(CW\*(C`pairgrep\*(C'\fR aliases \f(CW$a\fR and \&\f(CW$b\fR to elements of the given list. Any modifications of it by the code block will be visible to the caller. .ie n .SS "( $key, $val ) = pairfirst { \s-1BLOCK \s0} @kvlist" .el .SS "( \f(CW$key\fP, \f(CW$val\fP ) = pairfirst { \s-1BLOCK \s0} \f(CW@kvlist\fP" .IX Subsection "( $key, $val ) = pairfirst { BLOCK } @kvlist" .ie n .SS "$found = pairfirst { \s-1BLOCK \s0} @kvlist" .el .SS "\f(CW$found\fP = pairfirst { \s-1BLOCK \s0} \f(CW@kvlist\fP" .IX Subsection "$found = pairfirst { BLOCK } @kvlist" Similar to the \f(CW\*(C`first\*(C'\fR function, but interprets the given list as an even-sized list of pairs. It invokes the \f(CW\*(C`BLOCK\*(C'\fR multiple times, in scalar context, with \f(CW$a\fR and \f(CW$b\fR set to successive pairs of values from the \&\f(CW@kvlist\fR. .PP Returns the first pair of values from the list for which the \f(CW\*(C`BLOCK\*(C'\fR 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. .PP .Vb 1 \& ( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist .Ve .PP As with \f(CW\*(C`grep\*(C'\fR aliasing \f(CW$_\fR to list elements, \f(CW\*(C`pairfirst\*(C'\fR aliases \f(CW$a\fR and \&\f(CW$b\fR to elements of the given list. Any modifications of it by the code block will be visible to the caller. .ie n .SS "@list = pairmap { \s-1BLOCK \s0} @kvlist" .el .SS "\f(CW@list\fP = pairmap { \s-1BLOCK \s0} \f(CW@kvlist\fP" .IX Subsection "@list = pairmap { BLOCK } @kvlist" .ie n .SS "$count = pairmap { \s-1BLOCK \s0} @kvlist" .el .SS "\f(CW$count\fP = pairmap { \s-1BLOCK \s0} \f(CW@kvlist\fP" .IX Subsection "$count = pairmap { BLOCK } @kvlist" Similar to perl's \f(CW\*(C`map\*(C'\fR keyword, but interprets the given list as an even-sized list of pairs. It invokes the \f(CW\*(C`BLOCK\*(C'\fR multiple times, in list context, with \f(CW$a\fR and \f(CW$b\fR set to successive pairs of values from the \&\f(CW@kvlist\fR. .PP Returns the concatenation of all the values returned by the \f(CW\*(C`BLOCK\*(C'\fR in list context, or the count of the number of items that would have been returned in scalar context. .PP .Vb 1 \& @result = pairmap { "The key $a has value $b" } @kvlist .Ve .PP As with \f(CW\*(C`map\*(C'\fR aliasing \f(CW$_\fR to list elements, \f(CW\*(C`pairmap\*(C'\fR aliases \f(CW$a\fR and \&\f(CW$b\fR to elements of the given list. Any modifications of it by the code block will be visible to the caller. .ie n .SS "@pairs = pairs @kvlist" .el .SS "\f(CW@pairs\fP = pairs \f(CW@kvlist\fP" .IX Subsection "@pairs = pairs @kvlist" A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of \s-1ARRAY\s0 references, each containing two items from the given list. It is a more efficient version of .PP .Vb 1 \& @pairs = pairmap { [ $a, $b ] } @kvlist .Ve .PP It is most convenient to use in a \f(CW\*(C`foreach\*(C'\fR loop, for example: .PP .Vb 4 \& foreach ( pairs @KVLIST ) { \& my ( $key, $value ) = @$_; \& ... \& } .Ve .ie n .SS "@keys = pairkeys @kvlist" .el .SS "\f(CW@keys\fP = pairkeys \f(CW@kvlist\fP" .IX Subsection "@keys = pairkeys @kvlist" 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 .PP .Vb 1 \& @keys = pairmap { $a } @kvlist .Ve .ie n .SS "@values = pairvalues @kvlist" .el .SS "\f(CW@values\fP = pairvalues \f(CW@kvlist\fP" .IX Subsection "@values = pairvalues @kvlist" 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 .PP .Vb 1 \& @values = pairmap { $b } @kvlist .Ve .SH "OTHER FUNCTIONS" .IX Header "OTHER FUNCTIONS" .ie n .SS "@values = shuffle @values" .el .SS "\f(CW@values\fP = shuffle \f(CW@values\fP" .IX Subsection "@values = shuffle @values" Returns the values of the input in a random order .PP .Vb 1 \& @cards = shuffle 0..51 # 0..51 in a random order .Ve .SH "KNOWN BUGS" .IX Header "KNOWN BUGS" With perl versions prior to 5.005 there are some cases where reduce will return an incorrect result. This will show up as test 7 of reduce.t failing. .SH "SUGGESTED ADDITIONS" .IX Header "SUGGESTED ADDITIONS" 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 .PP .Vb 1 \& # How many elements are true \& \& sub true { scalar grep { $_ } @_ } \& \& # How many elements are false \& \& sub false { scalar grep { !$_ } @_ } .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Scalar::Util, List::MoreUtils .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (c) 1997\-2007 Graham Barr . All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP Recent additions and current maintenance by Paul Evans, .