📄 list::util.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...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" '''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 (.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..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" 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 3".TH List::Util 3 "2007-12-18" "perl v5.10.0" "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 haveexpressed would be nice to have in the perl core, but the usage wouldnot really be high enough to warrant the use of a keyword, and the sizeso small such that being individual extensions would be wasteful..PPBy default \f(CW\*(C`List::Util\*(C'\fR does not export any subroutines. Thesubroutines defined are.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 elementof \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..Sp.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.SpThis function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this.Sp.Vb 1\& $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list.Ve.Spfor example \fIwanted()\fR could be \fIdefined()\fR which would return the firstdefined value in \f(CW@list\fR.IP "max \s-1LIST\s0" 4.IX Item "max LIST"Returns the entry in the list with the highest numerical value. If thelist is empty then \f(CW\*(C`undef\*(C'\fR is returned..Sp.Vb 3\& $foo = max 1..10 # 10\& $foo = max 3,9,12 # 12\& $foo = max @bar, @baz # whatever.Ve.SpThis function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this.Sp.Vb 1\& $foo = reduce { $a > $b ? $a : $b } 1..10.Ve.IP "maxstr \s-1LIST\s0" 4.IX Item "maxstr LIST"Similar to \f(CW\*(C`max\*(C'\fR, but treats all the entries in the list as stringsand 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..Sp.Vb 3\& $foo = maxstr \*(AqA\*(Aq..\*(AqZ\*(Aq # \*(AqZ\*(Aq\& $foo = maxstr "hello","world" # "world"\& $foo = maxstr @bar, @baz # whatever.Ve.SpThis function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this.Sp.Vb 1\& $foo = reduce { $a gt $b ? $a : $b } \*(AqA\*(Aq..\*(AqZ\*(Aq.Ve.IP "min \s-1LIST\s0" 4.IX Item "min LIST"Similar to \f(CW\*(C`max\*(C'\fR but returns the entry in the list with the lowestnumerical value. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned..Sp.Vb 3\& $foo = min 1..10 # 1\& $foo = min 3,9,12 # 3\& $foo = min @bar, @baz # whatever.Ve.SpThis function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this.Sp.Vb 1\& $foo = reduce { $a < $b ? $a : $b } 1..10.Ve.IP "minstr \s-1LIST\s0" 4.IX Item "minstr LIST"Similar to \f(CW\*(C`min\*(C'\fR, but treats all the entries in the list as stringsand 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..Sp.Vb 3\& $foo = minstr \*(AqA\*(Aq..\*(AqZ\*(Aq # \*(AqA\*(Aq\& $foo = minstr "hello","world" # "hello"\& $foo = minstr @bar, @baz # whatever.Ve.SpThis function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this.Sp.Vb 1\& $foo = reduce { $a lt $b ? $a : $b } \*(AqA\*(Aq..\*(AqZ\*(Aq.Ve.IP "reduce \s-1BLOCK\s0 \s-1LIST\s0" 4.IX Item "reduce BLOCK LIST"Reduces \s-1LIST\s0 by calling \s-1BLOCK\s0, 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\fRand \f(CW$b\fR set to the first two elements of the list, subsequentcalls will be done by setting \f(CW$a\fR to the result of the previouscall and \f(CW$b\fR to the next element in the list..SpReturns 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 thatelement is returned and \s-1BLOCK\s0 is not executed..Sp.Vb 4\& $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.Ve.IP "shuffle \s-1LIST\s0" 4.IX Item "shuffle LIST"Returns the elements of \s-1LIST\s0 in a random order.Sp.Vb 1\& @cards = shuffle 0..51 # 0..51 in a random order.Ve.IP "sum \s-1LIST\s0" 4.IX Item "sum LIST"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..Sp.Vb 3\& $foo = sum 1..10 # 55\& $foo = sum 3,9,12 # 24\& $foo = sum @bar, @baz # whatever.Ve.SpThis function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this.Sp.Vb 1\& $foo = reduce { $a + $b } 1..10.Ve.SH "KNOWN BUGS".IX Header "KNOWN BUGS"With perl versions prior to 5.005 there are some cases where reducewill return an incorrect result. This will show up as test 7 ofreduce.t failing..SH "SUGGESTED ADDITIONS".IX Header "SUGGESTED ADDITIONS"The following are additions that have been requested, but I have been reluctantto add due to them being very simple to implement in perl.PP.Vb 1\& # 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 { !$_ } @_ }.Ve.SH "SEE ALSO".IX Header "SEE ALSO"Scalar::Util, List::MoreUtils.SH "COPYRIGHT".IX Header "COPYRIGHT"Copyright (c) 1997\-2006 Graham Barr <gbarr@pobox.com>. All rights reserved.This program is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -