.\" 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::AllUtils 3pm" .TH List::AllUtils 3pm "2014-10-05" "perl v5.20.1" "User Contributed Perl Documentation" .\" 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::AllUtils \- Combines List::Util and List::MoreUtils in one bite\-sized package .SH "VERSION" .IX Header "VERSION" version 0.09 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use List::AllUtils qw( first any ); \& \& # _Everything_ from List::Util and List::MoreUtils \& use List::AllUtils qw( :all ); \& \& my @numbers = ( 1, 2, 3, 5, 7 ); \& # or don\*(Aqt import anything \& return List::AllUtils::first { $_ > 5 } @numbers; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Are you sick of trying to remember whether a particular helper is defined in List::Util or List::MoreUtils? I sure am. Now you don't have to remember. This module will export all of the functions that either of those two modules defines. .PP Note that all function documentation has been shamelessly copied from List::Util and List::MoreUtils. .SS "Which One Wins?" .IX Subsection "Which One Wins?" Recently, List::Util has started including some of the subs that used to only be in List::MoreUtils. This module always exports the version provided by List::Util. .PP The docs below come from List::Util 1.31 and List::MoreUtils 0.28. .SH "LIST-REDUCTION FUNCTIONS" .IX Header "LIST-REDUCTION FUNCTIONS" The following set of functions all reduce a list down to a single value. .SS "reduce \s-1BLOCK LIST\s0" .IX Subsection "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\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 \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. .PP .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 .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. .SS "first \s-1BLOCK LIST\s0" .IX Subsection "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. .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 .PP This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this .PP .Vb 1 \& $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list .Ve .PP for example \fIwanted()\fR could be \fIdefined()\fR which would return the first defined value in \f(CW@list\fR .SS "max \s-1LIST\s0" .IX Subsection "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 .PP This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this .PP .Vb 1 \& $foo = reduce { $a > $b ? $a : $b } 1..10 .Ve .SS "maxstr \s-1LIST\s0" .IX Subsection "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 .PP This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this .PP .Vb 1 \& $foo = reduce { $a gt $b ? $a : $b } \*(AqA\*(Aq..\*(AqZ\*(Aq .Ve .SS "min \s-1LIST\s0" .IX Subsection "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 .PP This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this .PP .Vb 1 \& $foo = reduce { $a < $b ? $a : $b } 1..10 .Ve .SS "minstr \s-1LIST\s0" .IX Subsection "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 .PP This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this .PP .Vb 1 \& $foo = reduce { $a lt $b ? $a : $b } \*(AqA\*(Aq..\*(AqZ\*(Aq .Ve .SS "sum \s-1LIST\s0" .IX Subsection "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. .PP .Vb 3 \& $foo = sum 1..10 # 55 \& $foo = sum 3,9,12 # 24 \& $foo = sum @bar, @baz # whatever .Ve .PP This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this .PP .Vb 1 \& $foo = reduce { $a + $b } 1..10 .Ve .SS "sum0 \s-1LIST\s0" .IX Subsection "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. .SS "pairgrep \s-1BLOCK KVLIST\s0" .IX Subsection "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 \s-1BLOCK\s0 multiple times, in scalar context, with \f(CW$a\fR and \f(CW$b\fR set to successive pairs of values from the \&\s-1KVLIST.\s0 .PP Returns an even-sized list of those pairs for which the \s-1BLOCK\s0 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 Similar to \f(CW\*(C`grep\*(C'\fR, \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. .SS "pairfirst \s-1BLOCK KVLIST\s0" .IX Subsection "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 \s-1BLOCK\s0 multiple times, in scalar context, with \f(CW$a\fR and \f(CW$b\fR set to successive pairs of values from the \&\s-1KVLIST.\s0 .PP Returns the first pair of values from the list for which the \s-1BLOCK\s0 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 Similar to \f(CW\*(C`grep\*(C'\fR, \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. .SS "pairmap \s-1BLOCK KVLIST\s0" .IX Subsection "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 \s-1BLOCK\s0 multiple times, in list context, with \f(CW$a\fR and \f(CW$b\fR set to successive pairs of values from the \&\s-1KVLIST.\s0 .PP Returns the concatenation of all the values returned by the \s-1BLOCK\s0 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 Similar to \f(CW\*(C`map\*(C'\fR, \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. .SS "pairs \s-1KVLIST\s0" .IX Subsection "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 \& 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 .SS "pairkeys \s-1KVLIST\s0" .IX Subsection "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 \& pairmap { $a } KVLIST .Ve .SS "pairvalues \s-1KVLIST\s0" .IX Subsection "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 \& pairmap { $b } KVLIST .Ve .SH "OTHER FUNCTIONS" .IX Header "OTHER FUNCTIONS" .SS "shuffle \s-1LIST\s0" .IX Subsection "shuffle LIST" Returns the elements of \s-1LIST\s0 in a random order .PP .Vb 1 \& @cards = shuffle 0..51 # 0..51 in a random order .Ve .SH "List::MoreUtils FUNCTIONS" .IX Header "List::MoreUtils FUNCTIONS" .SS "any \s-1BLOCK LIST\s0" .IX Subsection "any BLOCK LIST" Returns a true value if any item in \s-1LIST\s0 meets the criterion given through \&\s-1BLOCK.\s0 Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 2 \& print "At least one value undefined" \& if any { ! defined($_) } @list; .Ve .PP Returns false otherwise, or if \s-1LIST\s0 is empty. .SS "all \s-1BLOCK LIST\s0" .IX Subsection "all BLOCK LIST" Returns a true value if all items in \s-1LIST\s0 meet the criterion given through \&\s-1BLOCK,\s0 or if \s-1LIST\s0 is empty. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 2 \& print "All items defined" \& if all { defined($_) } @list; .Ve .PP Returns false otherwise. .SS "none \s-1BLOCK LIST\s0" .IX Subsection "none BLOCK LIST" Logically the negation of \f(CW\*(C`any\*(C'\fR. Returns a true value if no item in \s-1LIST\s0 meets the criterion given through \s-1BLOCK,\s0 or if \s-1LIST\s0 is empty. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 2 \& print "No value defined" \& if none { defined($_) } @list; .Ve .PP Returns false otherwise. .SS "notall \s-1BLOCK LIST\s0" .IX Subsection "notall BLOCK LIST" Logically the negation of \f(CW\*(C`all\*(C'\fR. Returns a true value if not all items in \s-1LIST\s0 meet the criterion given through \s-1BLOCK.\s0 Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 2 \& print "Not all values defined" \& if notall { defined($_) } @list; .Ve .PP Returns false otherwise, or if \s-1LIST\s0 is empty. .SS "true \s-1BLOCK LIST\s0" .IX Subsection "true BLOCK LIST" Counts the number of elements in \s-1LIST\s0 for which the criterion in \s-1BLOCK\s0 is true. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 1 \& printf "%i item(s) are defined", true { defined($_) } @list; .Ve .SS "false \s-1BLOCK LIST\s0" .IX Subsection "false BLOCK LIST" Counts the number of elements in \s-1LIST\s0 for which the criterion in \s-1BLOCK\s0 is false. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 1 \& printf "%i item(s) are not defined", false { defined($_) } @list; .Ve .SS "firstidx \s-1BLOCK LIST\s0" .IX Subsection "firstidx BLOCK LIST" .SS "first_index \s-1BLOCK LIST\s0" .IX Subsection "first_index BLOCK LIST" Returns the index of the first element in \s-1LIST\s0 for which the criterion in \s-1BLOCK\s0 is true. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 4 \& my @list = (1, 4, 3, 2, 4, 6); \& printf "item with index %i in list is 4", firstidx { $_ == 4 } @list; \& _\|_END_\|_ \& item with index 1 in list is 4 .Ve .PP Returns \f(CW\*(C`\-1\*(C'\fR if no such item could be found. .PP \&\f(CW\*(C`first_index\*(C'\fR is an alias for \f(CW\*(C`firstidx\*(C'\fR. .SS "lastidx \s-1BLOCK LIST\s0" .IX Subsection "lastidx BLOCK LIST" .SS "last_index \s-1BLOCK LIST\s0" .IX Subsection "last_index BLOCK LIST" Returns the index of the last element in \s-1LIST\s0 for which the criterion in \s-1BLOCK\s0 is true. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 4 \& my @list = (1, 4, 3, 2, 4, 6); \& printf "item with index %i in list is 4", lastidx { $_ == 4 } @list; \& _\|_END_\|_ \& item with index 4 in list is 4 .Ve .PP Returns \f(CW\*(C`\-1\*(C'\fR if no such item could be found. .PP \&\f(CW\*(C`last_index\*(C'\fR is an alias for \f(CW\*(C`lastidx\*(C'\fR. .SS "insert_after \s-1BLOCK VALUE LIST\s0" .IX Subsection "insert_after BLOCK VALUE LIST" Inserts \s-1VALUE\s0 after the first item in \s-1LIST\s0 for which the criterion in \s-1BLOCK\s0 is true. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn. .PP .Vb 5 \& my @list = qw/This is a list/; \& insert_after { $_ eq "a" } "longer" => @list; \& print "@list"; \& _\|_END_\|_ \& This is a longer list .Ve .SS "insert_after_string \s-1STRING VALUE LIST\s0" .IX Subsection "insert_after_string STRING VALUE LIST" Inserts \s-1VALUE\s0 after the first item in \s-1LIST\s0 which is equal to \s-1STRING.\s0 .PP .Vb 5 \& my @list = qw/This is a list/; \& insert_after_string "a", "longer" => @list; \& print "@list"; \& _\|_END_\|_ \& This is a longer list .Ve .SS "apply \s-1BLOCK LIST\s0" .IX Subsection "apply BLOCK LIST" Applies \s-1BLOCK\s0 to each item in \s-1LIST\s0 and returns a list of the values after \s-1BLOCK\s0 has been applied. In scalar context, the last element is returned. This function is similar to \f(CW\*(C`map\*(C'\fR but will not modify the elements of the input list: .PP .Vb 7 \& my @list = (1 .. 4); \& my @mult = apply { $_ *= 2 } @list; \& print "\e@list = @list\en"; \& print "\e@mult = @mult\en"; \& _\|_END_\|_ \& @list = 1 2 3 4 \& @mult = 2 4 6 8 .Ve .PP Think of it as syntactic sugar for .PP .Vb 1 \& for (my @mult = @list) { $_ *= 2 } .Ve .SS "before \s-1BLOCK LIST\s0" .IX Subsection "before BLOCK LIST" Returns a list of values of \s-1LIST\s0 up to (and not including) the point where \s-1BLOCK\s0 returns a true value. Sets \f(CW$_\fR for each element in \s-1LIST\s0 in turn. .SS "before_incl \s-1BLOCK LIST\s0" .IX Subsection "before_incl BLOCK LIST" Same as \f(CW\*(C`before\*(C'\fR but also includes the element for which \s-1BLOCK\s0 is true. .SS "after \s-1BLOCK LIST\s0" .IX Subsection "after BLOCK LIST" Returns a list of the values of \s-1LIST\s0 after (and not including) the point where \s-1BLOCK\s0 returns a true value. Sets \f(CW$_\fR for each element in \s-1LIST\s0 in turn. .PP .Vb 1 \& @x = after { $_ % 5 == 0 } (1..9); # returns 6, 7, 8, 9 .Ve .SS "after_incl \s-1BLOCK LIST\s0" .IX Subsection "after_incl BLOCK LIST" Same as \f(CW\*(C`after\*(C'\fR but also includes the element for which \s-1BLOCK\s0 is true. .SS "indexes \s-1BLOCK LIST\s0" .IX Subsection "indexes BLOCK LIST" Evaluates \s-1BLOCK\s0 for each element in \s-1LIST \s0(assigned to \f(CW$_\fR) and returns a list of the indices of those elements for which \s-1BLOCK\s0 returned a true value. This is just like \f(CW\*(C`grep\*(C'\fR only that it returns indices instead of values: .PP .Vb 1 \& @x = indexes { $_ % 2 == 0 } (1..10); # returns 1, 3, 5, 7, 9 .Ve .SS "firstval \s-1BLOCK LIST\s0" .IX Subsection "firstval BLOCK LIST" .SS "first_value \s-1BLOCK LIST\s0" .IX Subsection "first_value BLOCK LIST" Returns the first element in \s-1LIST\s0 for which \s-1BLOCK\s0 evaluates to true. Each element of \s-1LIST\s0 is set to \f(CW$_\fR in turn. Returns \f(CW\*(C`undef\*(C'\fR if no such element has been found. .PP \&\f(CW\*(C`first_val\*(C'\fR is an alias for \f(CW\*(C`firstval\*(C'\fR. .SS "lastval \s-1BLOCK LIST\s0" .IX Subsection "lastval BLOCK LIST" .SS "last_value \s-1BLOCK LIST\s0" .IX Subsection "last_value BLOCK LIST" Returns the last value in \s-1LIST\s0 for which \s-1BLOCK\s0 evaluates to true. Each element of \s-1LIST\s0 is set to \f(CW$_\fR in turn. Returns \f(CW\*(C`undef\*(C'\fR if no such element has been found. .PP \&\f(CW\*(C`last_val\*(C'\fR is an alias for \f(CW\*(C`lastval\*(C'\fR. .SS "pairwise \s-1BLOCK ARRAY1 ARRAY2\s0" .IX Subsection "pairwise BLOCK ARRAY1 ARRAY2" Evaluates \s-1BLOCK\s0 for each pair of elements in \s-1ARRAY1\s0 and \s-1ARRAY2\s0 and returns a new list consisting of \s-1BLOCK\s0's return values. The two elements are set to \f(CW$a\fR and \f(CW$b\fR. Note that those two are aliases to the original value so changing them will modify the input arrays. .PP .Vb 3 \& @a = (1 .. 5); \& @b = (11 .. 15); \& @x = pairwise { $a + $b } @a, @b; # returns 12, 14, 16, 18, 20 \& \& # mesh with pairwise \& @a = qw/a b c/; \& @b = qw/1 2 3/; \& @x = pairwise { ($a, $b) } @a, @b; # returns a, 1, b, 2, c, 3 .Ve .SS "each_array \s-1ARRAY1 ARRAY2 ...\s0" .IX Subsection "each_array ARRAY1 ARRAY2 ..." Creates an array iterator to return the elements of the list of arrays \s-1ARRAY1, ARRAY2\s0 throughout ARRAYn in turn. That is, the first time it is called, it returns the first element of each array. The next time, it returns the second elements. And so on, until all elements are exhausted. .PP This is useful for looping over more than one array at once: .PP .Vb 2 \& my $ea = each_array(@a, @b, @c); \& while ( my ($a, $b, $c) = $ea\->() ) { .... } .Ve .PP The iterator returns the empty list when it reached the end of all arrays. .PP If the iterator is passed an argument of '\f(CW\*(C`index\*(C'\fR', then it returns the index of the last fetched set of values, as a scalar. .SS "each_arrayref \s-1LIST\s0" .IX Subsection "each_arrayref LIST" Like each_array, but the arguments are references to arrays, not the plain arrays. .SS "natatime \s-1EXPR, LIST\s0" .IX Subsection "natatime EXPR, LIST" Creates an array iterator, for looping over an array in chunks of \&\f(CW$n\fR items at a time. (n at a time, get it?). An example is probably a better explanation than I could give in words. .PP Example: .PP .Vb 6 \& my @x = (\*(Aqa\*(Aq .. \*(Aqg\*(Aq); \& my $it = natatime 3, @x; \& while (my @vals = $it\->()) \& { \& print "@vals\en"; \& } .Ve .PP This prints .PP .Vb 3 \& a b c \& d e f \& g .Ve .SS "mesh \s-1ARRAY1 ARRAY2\s0 [ \s-1ARRAY3 ... \s0]" .IX Subsection "mesh ARRAY1 ARRAY2 [ ARRAY3 ... ]" .SS "zip \s-1ARRAY1 ARRAY2\s0 [ \s-1ARRAY3 ... \s0]" .IX Subsection "zip ARRAY1 ARRAY2 [ ARRAY3 ... ]" Returns a list consisting of the first elements of each array, then the second, then the third, etc, until all arrays are exhausted. .PP Examples: .PP .Vb 3 \& @x = qw/a b c d/; \& @y = qw/1 2 3 4/; \& @z = mesh @x, @y; # returns a, 1, b, 2, c, 3, d, 4 \& \& @a = (\*(Aqx\*(Aq); \& @b = (\*(Aq1\*(Aq, \*(Aq2\*(Aq); \& @c = qw/zip zap zot/; \& @d = mesh @a, @b, @c; # x, 1, zip, undef, 2, zap, undef, undef, zot .Ve .PP \&\f(CW\*(C`zip\*(C'\fR is an alias for \f(CW\*(C`mesh\*(C'\fR. .SS "uniq \s-1LIST\s0" .IX Subsection "uniq LIST" .SS "distinct \s-1LIST\s0" .IX Subsection "distinct LIST" Returns a new list by stripping duplicate values in \s-1LIST.\s0 The order of elements in the returned list is the same as in \s-1LIST.\s0 In scalar context, returns the number of unique elements in \s-1LIST.\s0 .PP .Vb 2 \& my @x = uniq 1, 1, 2, 2, 3, 5, 3, 4; # returns 1 2 3 5 4 \& my $x = uniq 1, 1, 2, 2, 3, 5, 3, 4; # returns 5 .Ve .SS "minmax \s-1LIST\s0" .IX Subsection "minmax LIST" Calculates the minimum and maximum of \s-1LIST\s0 and returns a two element list with the first element being the minimum and the second the maximum. Returns the empty list if \s-1LIST\s0 was empty. .PP The \f(CW\*(C`minmax\*(C'\fR algorithm differs from a naive iteration over the list where each element is compared to two values being the so far calculated min and max value in that it only requires 3n/2 \- 2 comparisons. Thus it is the most efficient possible algorithm. .PP However, the Perl implementation of it has some overhead simply due to the fact that there are more lines of Perl code involved. Therefore, \s-1LIST\s0 needs to be fairly big in order for \f(CW\*(C`minmax\*(C'\fR to win over a naive implementation. This limitation does not apply to the \s-1XS\s0 version. .SS "part \s-1BLOCK LIST\s0" .IX Subsection "part BLOCK LIST" Partitions \s-1LIST\s0 based on the return value of \s-1BLOCK\s0 which denotes into which partition the current value is put. .PP Returns a list of the partitions thusly created. Each partition created is a reference to an array. .PP .Vb 2 \& my $i = 0; \& my @part = part { $i++ % 2 } 1 .. 8; # returns [1, 3, 5, 7], [2, 4, 6, 8] .Ve .PP You can have a sparse list of partitions as well where non-set partitions will be undef: .PP .Vb 1 \& my @part = part { 2 } 1 .. 10; # returns undef, undef, [ 1 .. 10 ] .Ve .PP Be careful with negative values, though: .PP .Vb 3 \& my @part = part { \-1 } 1 .. 10; \& _\|_END_\|_ \& Modification of non\-creatable array value attempted, subscript \-1 ... .Ve .PP Negative values are only okay when they refer to a partition previously created: .PP .Vb 3 \& my @idx = ( 0, 1, \-1 ); \& my $i = 0; \& my @part = part { $idx[$++ % 3] } 1 .. 8; # [1, 4, 7], [2, 3, 5, 6, 8] .Ve .SH "EXPORTS" .IX Header "EXPORTS" This module exports nothing by default. You can import functions by name, or get everything with the \f(CW\*(C`:all\*(C'\fR tag. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\f(CW\*(C`List::Util\*(C'\fR and \f(CW\*(C`List::MoreUtils\*(C'\fR, obviously. .PP Also see \f(CW\*(C`Util::Any\*(C'\fR, which unifies many more util modules, and also lets you rename functions as part of the import. .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \&\f(CW\*(C`bug\-list\-allutils@rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "AUTHOR" .IX Header "AUTHOR" Dave Rolsky .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is Copyright (c) 2014 by Dave Rolsky. .PP This is free software, licensed under: .PP .Vb 1 \& The Artistic License 2.0 (GPL Compatible) .Ve