.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" 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 >0, 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 .\" ======================================================================== .\" .IX Title "List::AllUtils 3pm" .TH List::AllUtils 3pm "2021-09-22" "perl v5.32.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, List::SomeUtils and List::UtilsBy in one bite\-sized package .SH "VERSION" .IX Header "VERSION" version 0.19 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use List::AllUtils qw( first any ); \& \& # _Everything_ from List::Util, List::SomeUtils, and List::UtilsBy \& 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, List::SomeUtils or List::UtilsBy? I sure am. Now you don't have to remember. This module will export all of the functions that either of those three modules defines. .PP Note that all function documentation has been shamelessly copied from List::Util, List::SomeUtils and List::UtilsBy. .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::SomeUtils. Similarly, List::SomeUtils has some small overlap with List::UtilsBy. .PP \&\f(CW\*(C`List::AllUtils\*(C'\fR use to always favors the subroutine provided by List::Util, List::SomeUtils or List::UtilsBy in that order. However, as of List::Util 1.56, it included some functions, \f(CW\*(C`mesh\*(C'\fR and \f(CW\*(C`zip\*(C'\fR with the same name as List::SomeUtils functions, but different behavior. .PP So going forward, we will always prefer backwards compatibility. This means that \f(CW\*(C`mesh\*(C'\fR and \f(CW\*(C`zip\*(C'\fR will always come from List::SomeUtils. If other incompatible functions are added to List::Util, those will also be skipped in favor of the List::SomeUtils version. .PP The docs below come from List::Util 1.56, List::SomeUtils 0.58, and List::UtilsBy 0.11. .SH "WHAT IS EXPORTED?" .IX Header "WHAT IS EXPORTED?" All this module does is load List::Util, List::SomeUtils, and List::UtilsBy, and then re-export everything that they provide. That means that regardless of the documentation below, you will get any subroutine that your installed version provides. .SH "LIST-REDUCTION FUNCTIONS" .IX Header "LIST-REDUCTION FUNCTIONS" The following set of functions all apply a given block of code to a list of values. .SS "reduce" .IX Subsection "reduce" .Vb 1 \& $result = reduce { BLOCK } @list .Ve .PP 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 above example code blocks also suggest how to use \f(CW\*(C`reduce\*(C'\fR to build a more efficient combined version of one of these basic functions and a \f(CW\*(C`map\*(C'\fR block. For example, to find the total length of all the strings in a list, we could use .PP .Vb 1 \& $total = sum map { length } @strings; .Ve .PP However, this produces a list of temporary integer values as long as the original list of strings, only to reduce it down to a single value again. We can compute the same result more efficiently by using \f(CW\*(C`reduce\*(C'\fR with a code block that accumulates lengths by writing this instead as: .PP .Vb 1 \& $total = reduce { $a + length $b } 0, @strings .Ve .PP The other scalar-returning list reduction functions are all specialisations of this generic idea. .SS "reductions" .IX Subsection "reductions" .Vb 1 \& @results = reductions { BLOCK } @list .Ve .PP \&\fISince version 1.54.\fR .PP Similar to \f(CW\*(C`reduce\*(C'\fR except that it also returns the intermediate values along with the final result. As before, \f(CW$a\fR is set to the first element of the given list, and the \f(CW\*(C`BLOCK\*(C'\fR is then called once for remaining item in the list set into \f(CW$b\fR, with the result being captured for return as well as becoming the new value for \f(CW$a\fR. .PP The returned list will begin with the initial value for \f(CW$a\fR, followed by each return value from the block in order. The final value of the result will be identical to what the \f(CW\*(C`reduce\*(C'\fR function would have returned given the same block and list. .PP .Vb 2 \& reduce { "$a\-$b" } "a".."d" # "a\-b\-c\-d" \& reductions { "$a\-$b" } "a".."d" # "a", "a\-b", "a\-b\-c", "a\-b\-c\-d" .Ve .SS "any" .IX Subsection "any" .Vb 1 \& my $bool = any { BLOCK } @list; .Ve .PP \&\fISince version 1.33.\fR .PP 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 .PP Note: Due to \s-1XS\s0 issues the block passed may be able to access the outer \f(CW@_\fR directly. This is not intentional and will break under debugger. .SS "all" .IX Subsection "all" .Vb 1 \& my $bool = all { BLOCK } @list; .Ve .PP \&\fISince version 1.33.\fR .PP Similar to \*(L"any\*(R", 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. .PP Note: Due to \s-1XS\s0 issues the block passed may be able to access the outer \f(CW@_\fR directly. This is not intentional and will break under debugger. .SS "none" .IX Subsection "none" .SS "notall" .IX Subsection "notall" .Vb 1 \& my $bool = none { BLOCK } @list; \& \& my $bool = notall { BLOCK } @list; .Ve .PP \&\fISince version 1.33.\fR .PP Similar to \*(L"any\*(R" and \*(L"all\*(R", but with the return sense inverted. \f(CW\*(C`none\*(C'\fR returns true only if no value in the \f(CW@list\fR causes the \f(CW\*(C`BLOCK\*(C'\fR to return true, and \f(CW\*(C`notall\*(C'\fR returns true only if not all of the values do. .PP Note: Due to \s-1XS\s0 issues the block passed may be able to access the outer \f(CW@_\fR directly. This is not intentional and will break under debugger. .SS "first" .IX Subsection "first" .Vb 1 \& my $val = first { BLOCK } @list; .Ve .PP 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 .SS "max" .IX Subsection "max" .Vb 1 \& my $num = max @list; .Ve .PP 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 .SS "maxstr" .IX Subsection "maxstr" .Vb 1 \& my $str = maxstr @list; .Ve .PP Similar to \*(L"max\*(R", 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 .SS "min" .IX Subsection "min" .Vb 1 \& my $num = min @list; .Ve .PP Similar to \*(L"max\*(R" 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 .SS "minstr" .IX Subsection "minstr" .Vb 1 \& my $str = minstr @list; .Ve .PP Similar to \*(L"min\*(R", 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 .SS "product" .IX Subsection "product" .Vb 1 \& my $num = product @list; .Ve .PP \&\fISince version 1.35.\fR .PP 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 .SS "sum" .IX Subsection "sum" .Vb 1 \& my $num_or_undef = sum @list; .Ve .PP 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 .SS "sum0" .IX Subsection "sum0" .Vb 1 \& my $num = sum0 @list; .Ve .PP \&\fISince version 1.26.\fR .PP Similar to \*(L"sum\*(R", 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. .PP \&\fB\s-1NOTE\s0\fR: At the time of writing, the following \f(CW\*(C`pair*\*(C'\fR functions that take a block do not modify the value of \f(CW$_\fR within the block, and instead operate using the \f(CW$a\fR and \f(CW$b\fR globals instead. This has turned out to be a poor design, as it precludes the ability to provide a \f(CW\*(C`pairsort\*(C'\fR function. Better would be to pass pair-like objects as 2\-element array references in \f(CW$_\fR, in a style similar to the return value of the \f(CW\*(C`pairs\*(C'\fR function. At some future version this behaviour may be added. .PP Until then, users are alerted \fB\s-1NOT\s0\fR to rely on the value of \f(CW$_\fR remaining unmodified between the outside and the inside of the control block. In particular, the following example is \fB\s-1UNSAFE\s0\fR: .PP .Vb 1 \& my @kvlist = ... \& \& foreach (qw( some keys here )) { \& my @items = pairgrep { $a eq $_ } @kvlist; \& ... \& } .Ve .PP Instead, write this using a lexical variable: .PP .Vb 4 \& foreach my $key (qw( some keys here )) { \& my @items = pairgrep { $a eq $key } @kvlist; \& ... \& } .Ve .SS "pairs" .IX Subsection "pairs" .Vb 1 \& my @pairs = pairs @kvlist; .Ve .PP \&\fISince version 1.29.\fR .PP A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of \f(CW\*(C`ARRAY\*(C'\fR 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 my $pair ( pairs @kvlist ) { \& my ( $key, $value ) = @$pair; \& ... \& } .Ve .PP Since version \f(CW1.39\fR these \f(CW\*(C`ARRAY\*(C'\fR references are blessed objects, recognising the two methods \f(CW\*(C`key\*(C'\fR and \f(CW\*(C`value\*(C'\fR. The following code is equivalent: .PP .Vb 5 \& foreach my $pair ( pairs @kvlist ) { \& my $key = $pair\->key; \& my $value = $pair\->value; \& ... \& } .Ve .PP Since version \f(CW1.51\fR they also have a \f(CW\*(C`TO_JSON\*(C'\fR method to ease serialisation. .SS "unpairs" .IX Subsection "unpairs" .Vb 1 \& my @kvlist = unpairs @pairs .Ve .PP \&\fISince version 1.42.\fR .PP The inverse function to \f(CW\*(C`pairs\*(C'\fR; this function takes a list of \f(CW\*(C`ARRAY\*(C'\fR references containing two elements each, and returns a flattened list of the two values from each of the pairs, in order. This is notionally equivalent to .PP .Vb 1 \& my @kvlist = map { @{$_}[0,1] } @pairs .Ve .PP except that it is implemented more efficiently internally. Specifically, for any input item it will extract exactly two values for the output list; using \&\f(CW\*(C`undef\*(C'\fR if the input array references are short. .PP Between \f(CW\*(C`pairs\*(C'\fR and \f(CW\*(C`unpairs\*(C'\fR, a higher-order list function can be used to operate on the pairs as single scalars; such as the following near-equivalents of the other \f(CW\*(C`pair*\*(C'\fR higher-order functions: .PP .Vb 2 \& @kvlist = unpairs grep { FUNC } pairs @kvlist \& # Like pairgrep, but takes $_ instead of $a and $b \& \& @kvlist = unpairs map { FUNC } pairs @kvlist \& # Like pairmap, but takes $_ instead of $a and $b .Ve .PP Note however that these versions will not behave as nicely in scalar context. .PP Finally, this technique can be used to implement a sort on a keyvalue pair list; e.g.: .PP .Vb 1 \& @kvlist = unpairs sort { $a\->key cmp $b\->key } pairs @kvlist .Ve .SS "pairkeys" .IX Subsection "pairkeys" .Vb 1 \& my @keys = pairkeys @kvlist; .Ve .PP \&\fISince version 1.29.\fR .PP 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 .SS "pairvalues" .IX Subsection "pairvalues" .Vb 1 \& my @values = pairvalues @kvlist; .Ve .PP \&\fISince version 1.29.\fR .PP 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 .SS "pairgrep" .IX Subsection "pairgrep" .Vb 1 \& my @kvlist = pairgrep { BLOCK } @kvlist; \& \& my $count = pairgrep { BLOCK } @kvlist; .Ve .PP \&\fISince version 1.29.\fR .PP 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. .SS "pairfirst" .IX Subsection "pairfirst" .Vb 1 \& my ( $key, $val ) = pairfirst { BLOCK } @kvlist; \& \& my $found = pairfirst { BLOCK } @kvlist; .Ve .PP \&\fISince version 1.30.\fR .PP Similar to the \*(L"first\*(R" 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. .SS "pairmap" .IX Subsection "pairmap" .Vb 1 \& my @list = pairmap { BLOCK } @kvlist; \& \& my $count = pairmap { BLOCK } @kvlist; .Ve .PP \&\fISince version 1.29.\fR .PP 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. .PP See \*(L"\s-1KNOWN BUGS\*(R"\s0 for a known-bug with \f(CW\*(C`pairmap\*(C'\fR, and a workaround. .SH "OTHER FUNCTIONS" .IX Header "OTHER FUNCTIONS" .SS "shuffle" .IX Subsection "shuffle" .Vb 1 \& my @values = shuffle @values; .Ve .PP Returns the values of the input in a random order .PP .Vb 1 \& @cards = shuffle 0..51 # 0..51 in a random order .Ve .PP This function is affected by the \f(CW$RAND\fR variable. .SS "sample" .IX Subsection "sample" .Vb 1 \& my @items = sample $count, @values .Ve .PP \&\fISince version 1.54.\fR .PP Randomly select the given number of elements from the input list. Any given position in the input list will be selected at most once. .PP If there are fewer than \f(CW$count\fR items in the list then the function will return once all of them have been randomly selected; effectively the function behaves similarly to \*(L"shuffle\*(R". .PP This function is affected by the \f(CW$RAND\fR variable. .SS "uniq" .IX Subsection "uniq" .Vb 1 \& my @subset = uniq @values .Ve .PP \&\fISince version 1.45.\fR .PP Filters a list of values to remove subsequent duplicates, as judged by a DWIM-ish string equality or \f(CW\*(C`undef\*(C'\fR test. Preserves the order of unique elements, and retains the first value of any duplicate set. .PP .Vb 1 \& my $count = uniq @values .Ve .PP In scalar context, returns the number of elements that would have been returned as a list. .PP The \f(CW\*(C`undef\*(C'\fR value is treated by this function as distinct from the empty string, and no warning will be produced. It is left as-is in the returned list. Subsequent \f(CW\*(C`undef\*(C'\fR values are still considered identical to the first, and will be removed. .SS "uniqint" .IX Subsection "uniqint" .Vb 1 \& my @subset = uniqint @values .Ve .PP \&\fISince version 1.55.\fR .PP Filters a list of values to remove subsequent duplicates, as judged by an integer numerical equality test. Preserves the order of unique elements, and retains the first value of any duplicate set. Values in the returned list will be coerced into integers. .PP .Vb 1 \& my $count = uniqint @values .Ve .PP In scalar context, returns the number of elements that would have been returned as a list. .PP Note that \f(CW\*(C`undef\*(C'\fR is treated much as other numerical operations treat it; it compares equal to zero but additionally produces a warning if such warnings are enabled (\f(CW\*(C`use warnings \*(Aquninitialized\*(Aq;\*(C'\fR). In addition, an \f(CW\*(C`undef\*(C'\fR in the returned list is coerced into a numerical zero, so that the entire list of values returned by \f(CW\*(C`uniqint\*(C'\fR are well-behaved as integers. .SS "uniqnum" .IX Subsection "uniqnum" .Vb 1 \& my @subset = uniqnum @values .Ve .PP \&\fISince version 1.44.\fR .PP Filters a list of values to remove subsequent duplicates, as judged by a numerical equality test. Preserves the order of unique elements, and retains the first value of any duplicate set. .PP .Vb 1 \& my $count = uniqnum @values .Ve .PP In scalar context, returns the number of elements that would have been returned as a list. .PP Note that \f(CW\*(C`undef\*(C'\fR is treated much as other numerical operations treat it; it compares equal to zero but additionally produces a warning if such warnings are enabled (\f(CW\*(C`use warnings \*(Aquninitialized\*(Aq;\*(C'\fR). In addition, an \f(CW\*(C`undef\*(C'\fR in the returned list is coerced into a numerical zero, so that the entire list of values returned by \f(CW\*(C`uniqnum\*(C'\fR are well-behaved as numbers. .PP Note also that multiple \s-1IEEE\s0 \f(CW\*(C`NaN\*(C'\fR values are treated as duplicates of each other, regardless of any differences in their payloads, and despite the fact that \f(CW\*(C`0+\*(AqNaN\*(Aq == 0+\*(AqNaN\*(Aq\*(C'\fR yields false. .SS "uniqstr" .IX Subsection "uniqstr" .Vb 1 \& my @subset = uniqstr @values .Ve .PP \&\fISince version 1.45.\fR .PP Filters a list of values to remove subsequent duplicates, as judged by a string equality test. Preserves the order of unique elements, and retains the first value of any duplicate set. .PP .Vb 1 \& my $count = uniqstr @values .Ve .PP In scalar context, returns the number of elements that would have been returned as a list. .PP Note that \f(CW\*(C`undef\*(C'\fR is treated much as other string operations treat it; it compares equal to the empty string but additionally produces a warning if such warnings are enabled (\f(CW\*(C`use warnings \*(Aquninitialized\*(Aq;\*(C'\fR). In addition, an \&\f(CW\*(C`undef\*(C'\fR in the returned list is coerced into an empty string, so that the entire list of values returned by \f(CW\*(C`uniqstr\*(C'\fR are well-behaved as strings. .SS "head" .IX Subsection "head" .Vb 1 \& my @values = head $size, @list; .Ve .PP \&\fISince version 1.50.\fR .PP Returns the first \f(CW$size\fR elements from \f(CW@list\fR. If \f(CW$size\fR is negative, returns all but the last \f(CW$size\fR elements from \f(CW@list\fR. .PP .Vb 2 \& @result = head 2, qw( foo bar baz ); \& # foo, bar \& \& @result = head \-2, qw( foo bar baz ); \& # foo .Ve .SS "tail" .IX Subsection "tail" .Vb 1 \& my @values = tail $size, @list; .Ve .PP \&\fISince version 1.50.\fR .PP Returns the last \f(CW$size\fR elements from \f(CW@list\fR. If \f(CW$size\fR is negative, returns all but the first \f(CW$size\fR elements from \f(CW@list\fR. .PP .Vb 2 \& @result = tail 2, qw( foo bar baz ); \& # bar, baz \& \& @result = tail \-2, qw( foo bar baz ); \& # baz .Ve .SH "List::SomeUtils FUNCTIONS" .IX Header "List::SomeUtils FUNCTIONS" .SS "Junctions" .IX Subsection "Junctions" \fI\fITreatment of an empty list\fI\fR .IX Subsection "Treatment of an empty list" .PP There are two schools of thought for how to evaluate a junction on an empty list: .IP "\(bu" 4 Reduction to an identity (boolean) .IP "\(bu" 4 Result is undefined (three-valued) .PP In the first case, the result of the junction applied to the empty list is determined by a mathematical reduction to an identity depending on whether the underlying comparison is \*(L"or\*(R" or \*(L"and\*(R". Conceptually: .PP .Vb 5 \& "any are true" "all are true" \& \-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 2 elements: A || B || 0 A && B && 1 \& 1 element: A || 0 A && 1 \& 0 elements: 0 1 .Ve .PP In the second case, three-value logic is desired, in which a junction applied to an empty list returns \f(CW\*(C`undef\*(C'\fR rather than true or false .PP Junctions with a \f(CW\*(C`_u\*(C'\fR suffix implement three-valued logic. Those without are boolean. .PP \fIall \s-1BLOCK LIST\s0\fR .IX Subsection "all BLOCK LIST" .PP \fIall_u \s-1BLOCK LIST\s0\fR .IX Subsection "all_u BLOCK LIST" .PP Returns a true value if 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 "All values are non\-negative" \& if all { $_ >= 0 } ($x, $y, $z); .Ve .PP For an empty \s-1LIST,\s0 \f(CW\*(C`all\*(C'\fR returns true (i.e. no values failed the condition) and \f(CW\*(C`all_u\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR. .PP Thus, \f(CW\*(C`all_u(@list)\*(C'\fR is equivalent to \f(CW\*(C`@list ? all(@list) : undef\*(C'\fR. .PP \&\fBNote\fR: because Perl treats \f(CW\*(C`undef\*(C'\fR as false, you must check the return value of \f(CW\*(C`all_u\*(C'\fR with \f(CW\*(C`defined\*(C'\fR or you will get the opposite result of what you expect. .PP \fIany \s-1BLOCK LIST\s0\fR .IX Subsection "any BLOCK LIST" .PP \fIany_u \s-1BLOCK LIST\s0\fR .IX Subsection "any_u BLOCK LIST" .PP 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 non\-negative value" \& if any { $_ >= 0 } ($x, $y, $z); .Ve .PP For an empty \s-1LIST,\s0 \f(CW\*(C`any\*(C'\fR returns false and \f(CW\*(C`any_u\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR. .PP Thus, \f(CW\*(C`any_u(@list)\*(C'\fR is equivalent to \f(CW\*(C`@list ? any(@list) : undef\*(C'\fR. .PP \fInone \s-1BLOCK LIST\s0\fR .IX Subsection "none BLOCK LIST" .PP \fInone_u \s-1BLOCK LIST\s0\fR .IX Subsection "none_u BLOCK LIST" .PP 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 Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn: .PP .Vb 2 \& print "No non\-negative values" \& if none { $_ >= 0 } ($x, $y, $z); .Ve .PP For an empty \s-1LIST,\s0 \f(CW\*(C`none\*(C'\fR returns true (i.e. no values failed the condition) and \f(CW\*(C`none_u\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR. .PP Thus, \f(CW\*(C`none_u(@list)\*(C'\fR is equivalent to \f(CW\*(C`@list ? none(@list) : undef\*(C'\fR. .PP \&\fBNote\fR: because Perl treats \f(CW\*(C`undef\*(C'\fR as false, you must check the return value of \f(CW\*(C`none_u\*(C'\fR with \f(CW\*(C`defined\*(C'\fR or you will get the opposite result of what you expect. .PP \fInotall \s-1BLOCK LIST\s0\fR .IX Subsection "notall BLOCK LIST" .PP \fInotall_u \s-1BLOCK LIST\s0\fR .IX Subsection "notall_u BLOCK LIST" .PP 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 are non\-negative" \& if notall { $_ >= 0 } ($x, $y, $z); .Ve .PP For an empty \s-1LIST,\s0 \f(CW\*(C`notall\*(C'\fR returns false and \f(CW\*(C`notall_u\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR. .PP Thus, \f(CW\*(C`notall_u(@list)\*(C'\fR is equivalent to \f(CW\*(C`@list ? notall(@list) : undef\*(C'\fR. .PP \fIone \s-1BLOCK LIST\s0\fR .IX Subsection "one BLOCK LIST" .PP \fIone_u \s-1BLOCK LIST\s0\fR .IX Subsection "one_u BLOCK LIST" .PP Returns a true value if precisely one 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 "Precisely one value defined" \& if one { defined($_) } @list; .Ve .PP Returns false otherwise. .PP For an empty \s-1LIST,\s0 \f(CW\*(C`one\*(C'\fR returns false and \f(CW\*(C`one_u\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR. .PP The expression \f(CW\*(C`one BLOCK LIST\*(C'\fR is almost equivalent to \&\f(CW\*(C`1 == true BLOCK LIST\*(C'\fR, except for short-cutting. Evaluation of \s-1BLOCK\s0 will immediately stop at the second true value. .SS "Transformation" .IX Subsection "Transformation" \fIapply \s-1BLOCK LIST\s0\fR .IX Subsection "apply BLOCK LIST" .PP Makes a copy of the list and then passes each element \fIfrom the copy\fR to the \&\s-1BLOCK.\s0 Any changes or assignments to \f(CW$_\fR in the \s-1BLOCK\s0 will only affect the elements of the new list. However, if \f(CW$_\fR is a reference then changes to the referenced value will be seen in both the original and new list. .PP 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 .PP Note that you must alter \f(CW$_\fR directly inside \s-1BLOCK\s0 in order for changes to make effect. New value returned from the \s-1BLOCK\s0 are ignored: .PP .Vb 2 \& # @new is identical to @list. \& my @new = apply { $_ * 2 } @list; \& \& # @new is different from @list \& my @new = apply { $_ =* 2 } @list; .Ve .PP \fIinsert_after \s-1BLOCK VALUE LIST\s0\fR .IX Subsection "insert_after BLOCK VALUE LIST" .PP 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 .PP \fIinsert_after_string \s-1STRING VALUE LIST\s0\fR .IX Subsection "insert_after_string STRING VALUE LIST" .PP 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 .PP \fIpairwise \s-1BLOCK ARRAY1 ARRAY2\s0\fR .IX Subsection "pairwise BLOCK ARRAY1 ARRAY2" .PP 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 .PP \fImesh \s-1ARRAY1 ARRAY2\s0 [ \s-1ARRAY3 ...\s0 ]\fR .IX Subsection "mesh ARRAY1 ARRAY2 [ ARRAY3 ... ]" .PP \fIzip \s-1ARRAY1 ARRAY2\s0 [ \s-1ARRAY3 ...\s0 ]\fR .IX Subsection "zip ARRAY1 ARRAY2 [ ARRAY3 ... ]" .PP 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. .PP \fIuniq \s-1LIST\s0\fR .IX Subsection "uniq LIST" .PP \fIdistinct \s-1LIST\s0\fR .IX Subsection "distinct LIST" .PP Returns a new list by stripping duplicate values in \s-1LIST\s0 by comparing the values as hash keys, except that undef is considered separate from ''. 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 8 \& 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 \& # returns "Mike", "Michael", "Richard", "Rick" \& my @n = distinct "Mike", "Michael", "Richard", "Rick", "Michael", "Rick" \& # returns \*(Aq\*(Aq, undef, \*(AqS1\*(Aq, A5\*(Aq \& my @s = distinct \*(Aq\*(Aq, undef, \*(AqS1\*(Aq, \*(AqA5\*(Aq \& # returns \*(Aq\*(Aq, undef, \*(AqS1\*(Aq, A5\*(Aq \& my @w = uniq undef, \*(Aq\*(Aq, \*(AqS1\*(Aq, \*(AqA5\*(Aq .Ve .PP \&\f(CW\*(C`distinct\*(C'\fR is an alias for \f(CW\*(C`uniq\*(C'\fR. .PP \&\fBRT#49800\fR can be used to give feedback about this behavior. .PP \fIsingleton\fR .IX Subsection "singleton" .PP Returns a new list by stripping values in \s-1LIST\s0 occurring more than once by comparing the values as hash keys, except that undef is considered separate from ''. The order of elements in the returned list is the same as in \s-1LIST.\s0 In scalar context, returns the number of elements occurring only once in \s-1LIST.\s0 .PP .Vb 1 \& my @x = singleton 1,1,2,2,3,4,5 # returns 3 4 5 .Ve .SS "Partitioning" .IX Subsection "Partitioning" \fIafter \s-1BLOCK LIST\s0\fR .IX Subsection "after BLOCK LIST" .PP 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 .PP \fIafter_incl \s-1BLOCK LIST\s0\fR .IX Subsection "after_incl BLOCK LIST" .PP Same as \f(CW\*(C`after\*(C'\fR but also includes the element for which \s-1BLOCK\s0 is true. .PP \fIbefore \s-1BLOCK LIST\s0\fR .IX Subsection "before BLOCK LIST" .PP 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. .PP \fIbefore_incl \s-1BLOCK LIST\s0\fR .IX Subsection "before_incl BLOCK LIST" .PP Same as \f(CW\*(C`before\*(C'\fR but also includes the element for which \s-1BLOCK\s0 is true. .PP \fIpart \s-1BLOCK LIST\s0\fR .IX Subsection "part BLOCK LIST" .PP 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 ok when they refer to a partition previously created: .PP .Vb 3 \& my @idx = ( 0, 1, \-1 ); \& my $i = 0; \& my @part = part { $idx[$i++ % 3] } 1 .. 8; # [1, 4, 7], [2, 3, 5, 6, 8] .Ve .SS "Iteration" .IX Subsection "Iteration" \fIeach_array \s-1ARRAY1 ARRAY2 ...\s0\fR .IX Subsection "each_array ARRAY1 ARRAY2 ..." .PP 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. .PP \fIeach_arrayref \s-1LIST\s0\fR .IX Subsection "each_arrayref LIST" .PP Like each_array, but the arguments are references to arrays, not the plain arrays. .PP \fInatatime \s-1EXPR, LIST\s0\fR .IX Subsection "natatime EXPR, LIST" .PP 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 "Searching" .IX Subsection "Searching" \fIbsearch \s-1BLOCK LIST\s0\fR .IX Subsection "bsearch BLOCK LIST" .PP Performs a binary search on \s-1LIST\s0 which must be a sorted list of values. \s-1BLOCK\s0 must return a negative value if the current element (stored in \f(CW$_\fR) is smaller, a positive value if it is bigger and zero if it matches. .PP Returns a boolean value in scalar context. In list context, it returns the element if it was found, otherwise the empty list. .PP \fIbsearchidx \s-1BLOCK LIST\s0\fR .IX Subsection "bsearchidx BLOCK LIST" .PP \fIbsearch_index \s-1BLOCK LIST\s0\fR .IX Subsection "bsearch_index BLOCK LIST" .PP Performs a binary search on \s-1LIST\s0 which must be a sorted list of values. \s-1BLOCK\s0 must return a negative value if the current element (stored in \f(CW$_\fR) is smaller, a positive value if it is bigger and zero if it matches. .PP Returns the index of found element, otherwise \f(CW\*(C`\-1\*(C'\fR. .PP \&\f(CW\*(C`bsearch_index\*(C'\fR is an alias for \f(CW\*(C`bsearchidx\*(C'\fR. .PP \fIfirstval \s-1BLOCK LIST\s0\fR .IX Subsection "firstval BLOCK LIST" .PP \fIfirst_value \s-1BLOCK LIST\s0\fR .IX Subsection "first_value BLOCK LIST" .PP 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_value\*(C'\fR is an alias for \f(CW\*(C`firstval\*(C'\fR. .PP \fIonlyval \s-1BLOCK LIST\s0\fR .IX Subsection "onlyval BLOCK LIST" .PP \fIonly_value \s-1BLOCK LIST\s0\fR .IX Subsection "only_value BLOCK LIST" .PP Returns the only element in \s-1LIST\s0 for which \s-1BLOCK\s0 evaluates to true. Sets \&\f(CW$_\fR for each item in \s-1LIST\s0 in turn. Returns \f(CW\*(C`undef\*(C'\fR if no such element has been found. .PP \&\f(CW\*(C`only_value\*(C'\fR is an alias for \f(CW\*(C`onlyval\*(C'\fR. .PP \fIlastval \s-1BLOCK LIST\s0\fR .IX Subsection "lastval BLOCK LIST" .PP \fIlast_value \s-1BLOCK LIST\s0\fR .IX Subsection "last_value BLOCK LIST" .PP 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_value\*(C'\fR is an alias for \f(CW\*(C`lastval\*(C'\fR. .PP \fIfirstres \s-1BLOCK LIST\s0\fR .IX Subsection "firstres BLOCK LIST" .PP \fIfirst_result \s-1BLOCK LIST\s0\fR .IX Subsection "first_result BLOCK LIST" .PP Returns the result of \s-1BLOCK\s0 for 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_result\*(C'\fR is an alias for \f(CW\*(C`firstres\*(C'\fR. .PP \fIonlyres \s-1BLOCK LIST\s0\fR .IX Subsection "onlyres BLOCK LIST" .PP \fIonly_result \s-1BLOCK LIST\s0\fR .IX Subsection "only_result BLOCK LIST" .PP Returns the result of \s-1BLOCK\s0 for the first element in \s-1LIST\s0 for which \s-1BLOCK\s0 evaluates to true. Sets \f(CW$_\fR for each item in \s-1LIST\s0 in turn. Returns \&\f(CW\*(C`undef\*(C'\fR if no such element has been found. .PP \&\f(CW\*(C`only_result\*(C'\fR is an alias for \f(CW\*(C`onlyres\*(C'\fR. .PP \fIlastres \s-1BLOCK LIST\s0\fR .IX Subsection "lastres BLOCK LIST" .PP \fIlast_result \s-1BLOCK LIST\s0\fR .IX Subsection "last_result BLOCK LIST" .PP Returns the result of \s-1BLOCK\s0 for the last 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`last_result\*(C'\fR is an alias for \f(CW\*(C`lastres\*(C'\fR. .PP \fIindexes \s-1BLOCK LIST\s0\fR .IX Subsection "indexes BLOCK LIST" .PP 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 .PP \fIfirstidx \s-1BLOCK LIST\s0\fR .IX Subsection "firstidx BLOCK LIST" .PP \fIfirst_index \s-1BLOCK LIST\s0\fR .IX Subsection "first_index BLOCK LIST" .PP 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. .PP \fIonlyidx \s-1BLOCK LIST\s0\fR .IX Subsection "onlyidx BLOCK LIST" .PP \fIonly_index \s-1BLOCK LIST\s0\fR .IX Subsection "only_index BLOCK LIST" .PP Returns the index of the only 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, 3, 4, 3, 2, 4); \& printf "uniqe index of item 2 in list is %i", onlyidx { $_ == 2 } @list; \& _\|_END_\|_ \& unique index of item 2 in list is 4 .Ve .PP Returns \f(CW\*(C`\-1\*(C'\fR if either no such item or more than one of these has been found. .PP \&\f(CW\*(C`only_index\*(C'\fR is an alias for \f(CW\*(C`onlyidx\*(C'\fR. .PP \fIlastidx \s-1BLOCK LIST\s0\fR .IX Subsection "lastidx BLOCK LIST" .PP \fIlast_index \s-1BLOCK LIST\s0\fR .IX Subsection "last_index BLOCK LIST" .PP 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 "Sorting" .IX Subsection "Sorting" \fIsort_by \s-1BLOCK LIST\s0\fR .IX Subsection "sort_by BLOCK LIST" .PP Returns the list of values sorted according to the string values returned by the \&\s-1KEYFUNC\s0 block or function. A typical use of this may be to sort objects according to the string value of some accessor, such as .PP .Vb 1 \& sort_by { $_\->name } @people .Ve .PP The key function is called in scalar context, being passed each value in turn as both \f(CW$_\fR and the only argument in the parameters, \f(CW@_\fR. The values are then sorted according to string comparisons on the values returned. This is equivalent to .PP .Vb 1 \& sort { $a\->name cmp $b\->name } @people .Ve .PP except that it guarantees the name accessor will be executed only once per value. One interesting use-case is to sort strings which may have numbers embedded in them \&\*(L"naturally\*(R", rather than lexically. .PP .Vb 1 \& sort_by { s/(\ed+)/sprintf "%09d", $1/eg; $_ } @strings .Ve .PP This sorts strings by generating sort keys which zero-pad the embedded numbers to some level (9 digits in this case), helping to ensure the lexical sort puts them in the correct order. .PP \fInsort_by \s-1BLOCK LIST\s0\fR .IX Subsection "nsort_by BLOCK LIST" .PP Similar to sort_by but compares its key values numerically. .SS "Counting and calculation" .IX Subsection "Counting and calculation" \fItrue \s-1BLOCK LIST\s0\fR .IX Subsection "true BLOCK LIST" .PP 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 .PP \fIfalse \s-1BLOCK LIST\s0\fR .IX Subsection "false BLOCK LIST" .PP 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 .PP \fIminmax \s-1LIST\s0\fR .IX Subsection "minmax LIST" .PP 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. .PP \fImode \s-1LIST\s0\fR .IX Subsection "mode LIST" .PP Calculates the most common items in the list and returns them as a list. This is effectively done by string comparisons, so references will be stringified. If they implement string overloading, this will be used. .PP If more than one item appears the same number of times in the list, all such items will be returned. For example, the mode of a unique list is the list itself. .PP This function returns a list in list context. In scalar context it returns a count indicating the number of modes in the list. .SH "List::UtilsBy FUNCTIONS" .IX Header "List::UtilsBy FUNCTIONS" All functions added since version 0.04 unless otherwise stated, as the original names for earlier versions were renamed. .SS "sort_by" .IX Subsection "sort_by" .Vb 1 \& @vals = sort_by { KEYFUNC } @vals .Ve .PP Returns the list of values sorted according to the string values returned by the \f(CW\*(C`KEYFUNC\*(C'\fR block or function. A typical use of this may be to sort objects according to the string value of some accessor, such as .PP .Vb 1 \& sort_by { $_\->name } @people .Ve .PP The key function is called in scalar context, being passed each value in turn as both \f(CW$_\fR and the only argument in the parameters, \f(CW@_\fR. The values are then sorted according to string comparisons on the values returned. .PP This is equivalent to .PP .Vb 1 \& sort { $a\->name cmp $b\->name } @people .Ve .PP except that it guarantees the \f(CW\*(C`name\*(C'\fR accessor will be executed only once per value. .PP One interesting use-case is to sort strings which may have numbers embedded in them \*(L"naturally\*(R", rather than lexically. .PP .Vb 1 \& sort_by { s/(\ed+)/sprintf "%09d", $1/eg; $_ } @strings .Ve .PP This sorts strings by generating sort keys which zero-pad the embedded numbers to some level (9 digits in this case), helping to ensure the lexical sort puts them in the correct order. .SS "nsort_by" .IX Subsection "nsort_by" .Vb 1 \& @vals = nsort_by { KEYFUNC } @vals .Ve .PP Similar to \*(L"sort_by\*(R" but compares its key values numerically. .SS "rev_sort_by" .IX Subsection "rev_sort_by" .SS "rev_nsort_by" .IX Subsection "rev_nsort_by" .Vb 1 \& @vals = rev_sort_by { KEYFUNC } @vals \& \& @vals = rev_nsort_by { KEYFUNC } @vals .Ve .PP \&\fISince version 0.06.\fR .PP Similar to \*(L"sort_by\*(R" and \*(L"nsort_by\*(R" but returns the list in the reverse order. Equivalent to .PP .Vb 1 \& @vals = reverse sort_by { KEYFUNC } @vals .Ve .PP except that these functions are slightly more efficient because they avoid the final \f(CW\*(C`reverse\*(C'\fR operation. .SS "max_by" .IX Subsection "max_by" .Vb 1 \& $optimal = max_by { KEYFUNC } @vals \& \& @optimal = max_by { KEYFUNC } @vals .Ve .PP Returns the (first) value from \f(CW@vals\fR that gives the numerically largest result from the key function. .PP .Vb 1 \& my $tallest = max_by { $_\->height } @people \& \& use File::stat qw( stat ); \& my $newest = max_by { stat($_)\->mtime } @files; .Ve .PP In scalar context, the first maximal value is returned. In list context, a list of all the maximal values is returned. This may be used to obtain positions other than the first, if order is significant. .PP If called on an empty list, an empty list is returned. .PP For symmetry with the \*(L"nsort_by\*(R" function, this is also provided under the name \f(CW\*(C`nmax_by\*(C'\fR since it behaves numerically. .SS "min_by" .IX Subsection "min_by" .Vb 1 \& $optimal = min_by { KEYFUNC } @vals \& \& @optimal = min_by { KEYFUNC } @vals .Ve .PP Similar to \*(L"max_by\*(R" but returns values which give the numerically smallest result from the key function. Also provided as \f(CW\*(C`nmin_by\*(C'\fR .SS "minmax_by" .IX Subsection "minmax_by" .Vb 1 \& ( $minimal, $maximal ) = minmax_by { KEYFUNC } @vals .Ve .PP \&\fISince version 0.11.\fR .PP Similar to calling both \*(L"min_by\*(R" and \*(L"max_by\*(R" with the same key function on the same list. This version is more efficient than calling the two other functions individually, as it has less work to perform overall. In the case of ties, only the first optimal element found in each case is returned. Also provided as \f(CW\*(C`nminmax_by\*(C'\fR. .SS "uniq_by" .IX Subsection "uniq_by" .Vb 1 \& @vals = uniq_by { KEYFUNC } @vals .Ve .PP Returns a list of the subset of values for which the key function block returns unique values. The first value yielding a particular key is chosen, subsequent values are rejected. .PP .Vb 1 \& my @some_fruit = uniq_by { $_\->colour } @fruit; .Ve .PP To select instead the last value per key, reverse the input list. If the order of the results is significant, don't forget to reverse the result as well: .PP .Vb 1 \& my @some_fruit = reverse uniq_by { $_\->colour } reverse @fruit; .Ve .PP Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner). .SS "partition_by" .IX Subsection "partition_by" .Vb 1 \& %parts = partition_by { KEYFUNC } @vals .Ve .PP Returns a key/value list of \s-1ARRAY\s0 refs containing all the original values distributed according to the result of the key function block. Each value will be an \s-1ARRAY\s0 ref containing all the values which returned the string from the key function, in their original order. .PP .Vb 1 \& my %balls_by_colour = partition_by { $_\->colour } @balls; .Ve .PP Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner). .SS "count_by" .IX Subsection "count_by" .Vb 1 \& %counts = count_by { KEYFUNC } @vals .Ve .PP \&\fISince version 0.07.\fR .PP Returns a key/value list of integers, giving the number of times the key function block returned the key, for each value in the list. .PP .Vb 1 \& my %count_of_balls = count_by { $_\->colour } @balls; .Ve .PP Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner). .SS "zip_by" .IX Subsection "zip_by" .Vb 1 \& @vals = zip_by { ITEMFUNC } \e@arr0, \e@arr1, \e@arr2,... .Ve .PP Returns a list of each of the values returned by the function block, when invoked with values from across each each of the given \s-1ARRAY\s0 references. Each value in the returned list will be the result of the function having been invoked with arguments at that position, from across each of the arrays given. .PP .Vb 1 \& my @transposition = zip_by { [ @_ ] } @matrix; \& \& my @names = zip_by { "$_[1], $_[0]" } \e@firstnames, \e@surnames; \& \& print zip_by { "$_[0] => $_[1]\en" } [ keys %hash ], [ values %hash ]; .Ve .PP If some of the arrays are shorter than others, the function will behave as if they had \f(CW\*(C`undef\*(C'\fR in the trailing positions. The following two lines are equivalent: .PP .Vb 2 \& zip_by { f(@_) } [ 1, 2, 3 ], [ "a", "b" ] \& f( 1, "a" ), f( 2, "b" ), f( 3, undef ) .Ve .PP The item function is called by \f(CW\*(C`map\*(C'\fR, so if it returns a list, the entire list is included in the result. This can be useful for example, for generating a hash from two separate lists of keys and values .PP .Vb 2 \& my %nums = zip_by { @_ } [qw( one two three )], [ 1, 2, 3 ]; \& # %nums = ( one => 1, two => 2, three => 3 ) .Ve .PP (A function having this behaviour is sometimes called \f(CW\*(C`zipWith\*(C'\fR, e.g. in Haskell, but that name would not fit the naming scheme used by this module). .SS "unzip_by" .IX Subsection "unzip_by" .Vb 1 \& $arr0, $arr1, $arr2, ... = unzip_by { ITEMFUNC } @vals .Ve .PP \&\fISince version 0.09.\fR .PP Returns a list of \s-1ARRAY\s0 references containing the values returned by the function block, when invoked for each of the values given in the input list. Each of the returned \s-1ARRAY\s0 references will contain the values returned at that corresponding position by the function block. That is, the first returned \&\s-1ARRAY\s0 reference will contain all the values returned in the first position by the function block, the second will contain all the values from the second position, and so on. .PP .Vb 1 \& my ( $firstnames, $lastnames ) = unzip_by { m/^(.*?) (.*)$/ } @names; .Ve .PP If the function returns lists of differing lengths, the result will be padded with \f(CW\*(C`undef\*(C'\fR in the missing elements. .PP This function is an inverse of \*(L"zip_by\*(R", if given a corresponding inverse function. .SS "extract_by" .IX Subsection "extract_by" .Vb 1 \& @vals = extract_by { SELECTFUNC } @arr .Ve .PP \&\fISince version 0.05.\fR .PP Removes elements from the referenced array on which the selection function returns true, and returns a list containing those elements. This function is similar to \f(CW\*(C`grep\*(C'\fR, except that it modifies the referenced array to remove the selected values from it, leaving only the unselected ones. .PP .Vb 1 \& my @red_balls = extract_by { $_\->color eq "red" } @balls; \& \& # Now there are no red balls in the @balls array .Ve .PP This function modifies a real array, unlike most of the other functions in this module. Because of this, it requires a real array, not just a list. .PP This function is implemented by invoking \f(CW\*(C`splice\*(C'\fR on the array, not by constructing a new list and assigning it. One result of this is that weak references will not be disturbed. .PP .Vb 1 \& extract_by { !defined $_ } @refs; .Ve .PP will leave weak references weakened in the \f(CW@refs\fR array, whereas .PP .Vb 1 \& @refs = grep { defined $_ } @refs; .Ve .PP will strengthen them all again. .SS "extract_first_by" .IX Subsection "extract_first_by" .Vb 1 \& $val = extract_first_by { SELECTFUNC } @arr .Ve .PP \&\fISince version 0.10.\fR .PP A hybrid between \*(L"extract_by\*(R" and \f(CW\*(C`List::Util::first\*(C'\fR. Removes the first element from the referenced array on which the selection function returns true, returning it. .PP As with \*(L"extract_by\*(R", this function requires a real array and not just a list, and is also implemented using \f(CW\*(C`splice\*(C'\fR so that weak references are not disturbed. .PP If this function fails to find a matching element, it will return an empty list in list context. This allows a caller to distinguish the case between no matching element, and the first matching element being \f(CW\*(C`undef\*(C'\fR. .SS "weighted_shuffle_by" .IX Subsection "weighted_shuffle_by" .Vb 1 \& @vals = weighted_shuffle_by { WEIGHTFUNC } @vals .Ve .PP \&\fISince version 0.07.\fR .PP Returns the list of values shuffled into a random order. The randomisation is not uniform, but weighted by the value returned by the \f(CW\*(C`WEIGHTFUNC\*(C'\fR. The probabilty of each item being returned first will be distributed with the distribution of the weights, and so on recursively for the remaining items. .SS "bundle_by" .IX Subsection "bundle_by" .Vb 1 \& @vals = bundle_by { BLOCKFUNC } $number, @vals .Ve .PP \&\fISince version 0.07.\fR .PP Similar to a regular \f(CW\*(C`map\*(C'\fR functional, returns a list of the values returned by \f(CW\*(C`BLOCKFUNC\*(C'\fR. Values from the input list are given to the block function in bundles of \f(CW$number\fR. .PP If given a list of values whose length does not evenly divide by \f(CW$number\fR, the final call will be passed fewer elements than the others. .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" List::Util, List::SomeUtils and List::UtilsBy, 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. .PP Bugs may be submitted at . .PP I am also usually active on \s-1IRC\s0 as 'autarch' on \f(CW\*(C`irc://irc.perl.org\*(C'\fR. .SH "SOURCE" .IX Header "SOURCE" The source code repository for List-AllUtils can be found at . .SH "DONATIONS" .IX Header "DONATIONS" If you'd like to thank me for the work I've done on this module, please consider making a \*(L"donation\*(R" to me via PayPal. I spend a lot of free time creating free software, and would appreciate any support you'd care to offer. .PP Please note that \fBI am not suggesting that you must do this\fR in order for me to continue working on this particular software. I will continue to do so, inasmuch as I have in the past, for as long as it interests me. .PP Similarly, a donation made in this way will probably not make me work on this software much more, unless I get so many donations that I can consider working on free software full time (let's all have a chuckle at that together). .PP To donate, log into PayPal and send money to autarch@urth.org, or use the button at . .SH "AUTHOR" .IX Header "AUTHOR" Dave Rolsky .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Andy Jack .IP "\(bu" 4 Dave Jacoby .IP "\(bu" 4 Karen Etheridge .IP "\(bu" 4 Olaf Alders .IP "\(bu" 4 Ricardo Signes .IP "\(bu" 4 Yanick Champoux .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is Copyright (c) 2021 by Dave Rolsky. .PP This is free software, licensed under: .PP .Vb 1 \& The Artistic License 2.0 (GPL Compatible) .Ve .PP The full text of the license can be found in the \&\fI\s-1LICENSE\s0\fR file included with this distribution.