.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
.\" ========================================================================
.de Sh \" Subsection heading
.de Sp \" Vertical space (when we can't use .PP)
.de Vb \" Begin verbatim text
.de Ve \" End verbatim text
.\" 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. | will give a
.\" real vertical bar. \*(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<>.
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
. 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
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
. tm Index:\\$1\t\\n%\t"\\$2"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.\" 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
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. \" simple accents for nroff and troff
. 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 \
.\" ========================================================================
.TH List::Util 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
List::Util \- A selection of general\-utility list subroutines
\& use List::Util qw(first max maxstr min minstr reduce shuffle sum);
\&\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.
By default \f(CW\*(C`List::Util\*(C'\fR does not export any subroutines. The
.IP "first \s-1BLOCK\s0 \s-1LIST\s0" 4
.IX Item "first BLOCK LIST"
Similar to \f(CW\*(C`grep\*(C'\fR in that it evaluates \s-1BLOCK\s0 setting \f(CW$_\fR to each element
of \s-1LIST\s0 in turn. \f(CW\*(C`first\*(C'\fR returns the first element where the result from
\&\s-1BLOCK\s0 is a true value. If \s-1BLOCK\s0 never returns true or \s-1LIST\s0 was empty then
\&\f(CW\*(C`undef\*(C'\fR is returned.
\& $foo = first { defined($_) } @list # first defined value in @list
\& $foo = first { $_ > $value } @list # first value in @list which
\& # is greater than $value
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
\& $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list
for example \fIwanted()\fR could be \fIdefined()\fR which would return the first
defined value in \f(CW@list\fR
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.
\& $foo = max 3,9,12 # 12
\& $foo = max @bar, @baz # whatever
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
\& $foo = reduce { $a > $b ? $a : $b } 1..10
.IP "maxstr \s-1LIST\s0" 4
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.
\& $foo = maxstr 'A'..'Z' # 'Z'
\& $foo = maxstr "hello","world" # "world"
\& $foo = maxstr @bar, @baz # whatever
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
\& $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z'
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.
\& $foo = min @bar, @baz # whatever
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
\& $foo = reduce { $a < $b ? $a : $b } 1..10
.IP "minstr \s-1LIST\s0" 4
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.
\& $foo = minstr 'A'..'Z' # 'A'
\& $foo = minstr "hello","world" # "hello"
\& $foo = minstr @bar, @baz # whatever
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
\& $foo = reduce { $a lt $b ? $a : $b } 'A'..'Z'
.IP "reduce \s-1BLOCK\s0 \s-1LIST\s0" 4
.IX Item "reduce BLOCK LIST"
Reduces \s-1LIST\s0 by calling \s-1BLOCK\s0 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
Returns the result of the last call to \s-1BLOCK\s0. If \s-1LIST\s0 is empty then
\&\f(CW\*(C`undef\*(C'\fR is returned. If \s-1LIST\s0 only contains one element then that
element is returned and \s-1BLOCK\s0 is not executed.
\& $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
.IP "shuffle \s-1LIST\s0" 4
Returns the elements of \s-1LIST\s0 in a random order
\& @cards = shuffle 0..51 # 0..51 in a random order
Returns the sum of all the elements in \s-1LIST\s0. If \s-1LIST\s0 is empty then
\&\f(CW\*(C`undef\*(C'\fR is returned.
\& $foo = sum 3,9,12 # 24
\& $foo = sum @bar, @baz # whatever
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
\& $foo = reduce { $a + $b } 1..10
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
.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
\& # One argument is true
\& sub any { $_ && return 1 for @_; 0 }
\& # All arguments are true
\& sub all { $_ || return 0 for @_; 1 }
\& # All arguments are false
\& sub none { $_ && return 0 for @_; 1 }
\& # One argument is false
\& sub notall { $_ || return 1 for @_; 0 }
\& # How many elements are true
\& sub true { scalar grep { $_ } @_ }
\& # How many elements are false
\& sub false { scalar grep { !$_ } @_ }
Copyright (c) 1997\-2005 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.