Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "List::Util 3" | |
132 | .TH List::Util 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | List::Util \- A selection of general\-utility list subroutines | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& use List::Util qw(first max maxstr min minstr reduce shuffle sum); | |
139 | .Ve | |
140 | .SH "DESCRIPTION" | |
141 | .IX Header "DESCRIPTION" | |
142 | \&\f(CW\*(C`List::Util\*(C'\fR contains a selection of subroutines that people have | |
143 | expressed would be nice to have in the perl core, but the usage would | |
144 | not really be high enough to warrant the use of a keyword, and the size | |
145 | so small such that being individual extensions would be wasteful. | |
146 | .PP | |
147 | By default \f(CW\*(C`List::Util\*(C'\fR does not export any subroutines. The | |
148 | subroutines defined are | |
149 | .IP "first \s-1BLOCK\s0 \s-1LIST\s0" 4 | |
150 | .IX Item "first BLOCK LIST" | |
151 | Similar to \f(CW\*(C`grep\*(C'\fR in that it evaluates \s-1BLOCK\s0 setting \f(CW$_\fR to each element | |
152 | of \s-1LIST\s0 in turn. \f(CW\*(C`first\*(C'\fR returns the first element where the result from | |
153 | \&\s-1BLOCK\s0 is a true value. If \s-1BLOCK\s0 never returns true or \s-1LIST\s0 was empty then | |
154 | \&\f(CW\*(C`undef\*(C'\fR is returned. | |
155 | .Sp | |
156 | .Vb 3 | |
157 | \& $foo = first { defined($_) } @list # first defined value in @list | |
158 | \& $foo = first { $_ > $value } @list # first value in @list which | |
159 | \& # is greater than $value | |
160 | .Ve | |
161 | .Sp | |
162 | This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this | |
163 | .Sp | |
164 | .Vb 1 | |
165 | \& $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list | |
166 | .Ve | |
167 | .Sp | |
168 | for example \fIwanted()\fR could be \fIdefined()\fR which would return the first | |
169 | defined value in \f(CW@list\fR | |
170 | .IP "max \s-1LIST\s0" 4 | |
171 | .IX Item "max LIST" | |
172 | Returns the entry in the list with the highest numerical value. If the | |
173 | list is empty then \f(CW\*(C`undef\*(C'\fR is returned. | |
174 | .Sp | |
175 | .Vb 3 | |
176 | \& $foo = max 1..10 # 10 | |
177 | \& $foo = max 3,9,12 # 12 | |
178 | \& $foo = max @bar, @baz # whatever | |
179 | .Ve | |
180 | .Sp | |
181 | This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this | |
182 | .Sp | |
183 | .Vb 1 | |
184 | \& $foo = reduce { $a > $b ? $a : $b } 1..10 | |
185 | .Ve | |
186 | .IP "maxstr \s-1LIST\s0" 4 | |
187 | .IX Item "maxstr LIST" | |
188 | Similar to \f(CW\*(C`max\*(C'\fR, but treats all the entries in the list as strings | |
189 | and returns the highest string as defined by the \f(CW\*(C`gt\*(C'\fR operator. | |
190 | If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. | |
191 | .Sp | |
192 | .Vb 3 | |
193 | \& $foo = maxstr 'A'..'Z' # 'Z' | |
194 | \& $foo = maxstr "hello","world" # "world" | |
195 | \& $foo = maxstr @bar, @baz # whatever | |
196 | .Ve | |
197 | .Sp | |
198 | This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this | |
199 | .Sp | |
200 | .Vb 1 | |
201 | \& $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' | |
202 | .Ve | |
203 | .IP "min \s-1LIST\s0" 4 | |
204 | .IX Item "min LIST" | |
205 | Similar to \f(CW\*(C`max\*(C'\fR but returns the entry in the list with the lowest | |
206 | numerical value. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. | |
207 | .Sp | |
208 | .Vb 3 | |
209 | \& $foo = min 1..10 # 1 | |
210 | \& $foo = min 3,9,12 # 3 | |
211 | \& $foo = min @bar, @baz # whatever | |
212 | .Ve | |
213 | .Sp | |
214 | This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this | |
215 | .Sp | |
216 | .Vb 1 | |
217 | \& $foo = reduce { $a < $b ? $a : $b } 1..10 | |
218 | .Ve | |
219 | .IP "minstr \s-1LIST\s0" 4 | |
220 | .IX Item "minstr LIST" | |
221 | Similar to \f(CW\*(C`min\*(C'\fR, but treats all the entries in the list as strings | |
222 | and returns the lowest string as defined by the \f(CW\*(C`lt\*(C'\fR operator. | |
223 | If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned. | |
224 | .Sp | |
225 | .Vb 3 | |
226 | \& $foo = minstr 'A'..'Z' # 'A' | |
227 | \& $foo = minstr "hello","world" # "hello" | |
228 | \& $foo = minstr @bar, @baz # whatever | |
229 | .Ve | |
230 | .Sp | |
231 | This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this | |
232 | .Sp | |
233 | .Vb 1 | |
234 | \& $foo = reduce { $a lt $b ? $a : $b } 'A'..'Z' | |
235 | .Ve | |
236 | .IP "reduce \s-1BLOCK\s0 \s-1LIST\s0" 4 | |
237 | .IX Item "reduce BLOCK LIST" | |
238 | Reduces \s-1LIST\s0 by calling \s-1BLOCK\s0 multiple times, setting \f(CW$a\fR and \f(CW$b\fR | |
239 | each time. The first call will be with \f(CW$a\fR and \f(CW$b\fR set to the first | |
240 | two elements of the list, subsequent calls will be done by | |
241 | setting \f(CW$a\fR to the result of the previous call and \f(CW$b\fR to the next | |
242 | element in the list. | |
243 | .Sp | |
244 | Returns the result of the last call to \s-1BLOCK\s0. If \s-1LIST\s0 is empty then | |
245 | \&\f(CW\*(C`undef\*(C'\fR is returned. If \s-1LIST\s0 only contains one element then that | |
246 | element is returned and \s-1BLOCK\s0 is not executed. | |
247 | .Sp | |
248 | .Vb 4 | |
249 | \& $foo = reduce { $a < $b ? $a : $b } 1..10 # min | |
250 | \& $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr | |
251 | \& $foo = reduce { $a + $b } 1 .. 10 # sum | |
252 | \& $foo = reduce { $a . $b } @bar # concat | |
253 | .Ve | |
254 | .IP "shuffle \s-1LIST\s0" 4 | |
255 | .IX Item "shuffle LIST" | |
256 | Returns the elements of \s-1LIST\s0 in a random order | |
257 | .Sp | |
258 | .Vb 1 | |
259 | \& @cards = shuffle 0..51 # 0..51 in a random order | |
260 | .Ve | |
261 | .IP "sum \s-1LIST\s0" 4 | |
262 | .IX Item "sum LIST" | |
263 | Returns the sum of all the elements in \s-1LIST\s0. | |
264 | .Sp | |
265 | .Vb 3 | |
266 | \& $foo = sum 1..10 # 55 | |
267 | \& $foo = sum 3,9,12 # 24 | |
268 | \& $foo = sum @bar, @baz # whatever | |
269 | .Ve | |
270 | .Sp | |
271 | This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this | |
272 | .Sp | |
273 | .Vb 1 | |
274 | \& $foo = reduce { $a + $b } 1..10 | |
275 | .Ve | |
276 | .SH "KNOWN BUGS" | |
277 | .IX Header "KNOWN BUGS" | |
278 | With perl versions prior to 5.005 there are some cases where reduce | |
279 | will return an incorrect result. This will show up as test 7 of | |
280 | reduce.t failing. | |
281 | .SH "SUGGESTED ADDITIONS" | |
282 | .IX Header "SUGGESTED ADDITIONS" | |
283 | The following are additions that have been requested, but I have been reluctant | |
284 | to add due to them being very simple to implement in perl | |
285 | .PP | |
286 | .Vb 1 | |
287 | \& # One argument is true | |
288 | .Ve | |
289 | .PP | |
290 | .Vb 1 | |
291 | \& sub any { $_ && return 1 for @_; 0 } | |
292 | .Ve | |
293 | .PP | |
294 | .Vb 1 | |
295 | \& # All arguments are true | |
296 | .Ve | |
297 | .PP | |
298 | .Vb 1 | |
299 | \& sub all { $_ || return 0 for @_; 1 } | |
300 | .Ve | |
301 | .PP | |
302 | .Vb 1 | |
303 | \& # All arguments are false | |
304 | .Ve | |
305 | .PP | |
306 | .Vb 1 | |
307 | \& sub none { $_ && return 0 for @_; 1 } | |
308 | .Ve | |
309 | .PP | |
310 | .Vb 1 | |
311 | \& # One argument is false | |
312 | .Ve | |
313 | .PP | |
314 | .Vb 1 | |
315 | \& sub notall { $_ || return 1 for @_; 0 } | |
316 | .Ve | |
317 | .PP | |
318 | .Vb 1 | |
319 | \& # How many elements are true | |
320 | .Ve | |
321 | .PP | |
322 | .Vb 1 | |
323 | \& sub true { scalar grep { $_ } @_ } | |
324 | .Ve | |
325 | .PP | |
326 | .Vb 1 | |
327 | \& # How many elements are false | |
328 | .Ve | |
329 | .PP | |
330 | .Vb 1 | |
331 | \& sub false { scalar grep { !$_ } @_ } | |
332 | .Ve | |
333 | .SH "COPYRIGHT" | |
334 | .IX Header "COPYRIGHT" | |
335 | Copyright (c) 1997\-2001 Graham Barr <gbarr@pobox.com>. All rights reserved. | |
336 | This program is free software; you can redistribute it and/or | |
337 | modify it under the same terms as Perl itself. |