.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" 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::SomeUtils 3pm" .TH List::SomeUtils 3pm "2022-12-31" "perl v5.36.0" "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::SomeUtils \- Provide the stuff missing in List::Util .SH "VERSION" .IX Header "VERSION" version 0.59 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& # import specific functions \& use List::SomeUtils qw( any uniq ); \& \& if ( any {/foo/} uniq @has_duplicates ) { \& \& # do stuff \& } \& \& # import everything \& use List::SomeUtils \*(Aq:all\*(Aq; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fBList::SomeUtils\fR provides some trivial but commonly needed functionality on lists which is not going to go into List::Util. .PP All of the below functions are implementable in only a couple of lines of Perl code. Using the functions from this module however should give slightly better performance as everything is implemented in C. The pure-Perl implementation of these functions only serves as a fallback in case the C portions of this module couldn't be compiled on this machine. .SH "WHY DOES THIS MODULE EXIST?" .IX Header "WHY DOES THIS MODULE EXIST?" You might wonder why this module exists when we already have List::MoreUtils. In fact, this module is (nearly) the same code as is found in \s-1LMU\s0 with no significant changes. However, the \s-1LMU\s0 distribution depends on several modules for configuration (to run the Makefile.PL) that some folks in the Perl community don't think are appropriate for a module high upstream in the \s-1CPAN\s0 river. .PP I (Dave Rolsky) don't have a strong opinion on this, but I \fIdo\fR like the functions provided by \s-1LMU,\s0 and I'm tired of getting patches and PRs to remove \&\s-1LMU\s0 from my code. .PP This distribution exists to let me use the functionality I like without having to get into tiring arguments about issues I don't really care about. .SH "EXPORTS" .IX Header "EXPORTS" .SS "Default behavior" .IX Subsection "Default behavior" Nothing by default. To import all of this module's symbols use the \f(CW\*(C`:all\*(C'\fR tag. Otherwise functions can be imported by name as usual: .PP .Vb 1 \& use List::SomeUtils \*(Aq:all\*(Aq; \& \& use List::SomeUtils qw{ any firstidx }; .Ve .PP Because historical changes to the \s-1API\s0 might make upgrading List::SomeUtils difficult for some projects, the legacy \s-1API\s0 is available via special import tags. .SH "FUNCTIONS" .IX Header "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 "unique 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 "MAINTENANCE" .IX Header "MAINTENANCE" The maintenance goal is to preserve the documented semantics of the \s-1API\s0; bug fixes that bring actual behavior in line with semantics are allowed. New \s-1API\s0 functions may be added over time. If a backwards incompatible change is unavoidable, we will attempt to provide support for the legacy \s-1API\s0 using the same export tag mechanism currently in place. .PP This module attempts to use few non-core dependencies. Non-core configuration and testing modules will be bundled when reasonable; run-time dependencies will be added only if they deliver substantial benefit. .SH "KNOWN ISSUES" .IX Header "KNOWN ISSUES" There is a problem with a bug in 5.6.x perls. It is a syntax error to write things like: .PP .Vb 1 \& my @x = apply { s/foo/bar/ } qw{ foo bar baz }; .Ve .PP It has to be written as either .PP .Vb 1 \& my @x = apply { s/foo/bar/ } \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq, \*(Aqbaz\*(Aq; .Ve .PP or .PP .Vb 1 \& my @x = apply { s/foo/bar/ } my @dummy = qw/foo bar baz/; .Ve .PP Perl 5.5.x and Perl 5.8.x don't suffer from this limitation. .PP If you have a functionality that you could imagine being in this module, please drop me a line. This module's policy will be less strict than List::Util's when it comes to additions as it isn't a core module. .PP When you report bugs, it would be nice if you could additionally give me the output of your program with the environment variable \f(CW\*(C`LIST_MOREUTILS_PP\*(C'\fR set to a true value. That way I know where to look for the problem (in \s-1XS,\s0 pure-Perl or possibly both). .SH "THANKS" .IX Header "THANKS" .SS "Tassilo von Parseval" .IX Subsection "Tassilo von Parseval" Credits go to a number of people: Steve Purkis for giving me namespace advice and James Keenan and Terrence Branno for their effort of keeping the \s-1CPAN\s0 tidier by making List::Util obsolete. .PP Brian McCauley suggested the inclusion of \fBapply()\fR and provided the pure-Perl implementation for it. .PP Eric J. Roode asked me to add all functions from his module \f(CW\*(C`List::SomeUtil\*(C'\fR into this one. With minor modifications, the pure-Perl implementations of those are by him. .PP The bunch of people who almost immediately pointed out the many problems with the glitchy 0.07 release (Slaven Rezic, Ron Savage, \s-1CPAN\s0 testers). .PP A particularly nasty memory leak was spotted by Thomas A. Lowery. .PP Lars Thegler made me aware of problems with older Perl versions. .PP Anno Siegel de-orphaned \fBeach_arrayref()\fR. .PP David Filmer made me aware of a problem in each_arrayref that could ultimately lead to a segfault. .PP Ricardo Signes suggested the inclusion of \fBpart()\fR and provided the Perl-implementation. .PP Robin Huston kindly fixed a bug in perl's \s-1MULTICALL API\s0 to make the XS-implementation of \fBpart()\fR work. .SS "Jens Rehsack" .IX Subsection "Jens Rehsack" Credits goes to all people contributing feedback during the v0.400 development releases. .PP Special thanks goes to David Golden who spent a lot of effort to develop a design to support current state of \s-1CPAN\s0 as well as ancient software somewhere in the dark. He also contributed a lot of patches to refactor the \s-1API\s0 frontend to welcome any user of List::SomeUtils \- from ancient past to recently last used. .PP Toby Inkster provided a lot of useful feedback for sane importer code and was a nice sounding board for \s-1API\s0 discussions. .PP Peter Rabbitson provided a sane git repository setup containing entire package history. .SH "TODO" .IX Header "TODO" A pile of requests from other people is still pending further processing in my mailbox. This includes: .IP "\(bu" 4 List::Util export pass-through .Sp Allow \fBList::SomeUtils\fR to pass-through the regular List::Util functions to end users only need to \f(CW\*(C`use\*(C'\fR the one module. .IP "\(bu" 4 uniq_by(&@) .Sp Use code-reference to extract a key based on which the uniqueness is determined. Suggested by Aaron Crane. .IP "\(bu" 4 delete_index .IP "\(bu" 4 random_item .IP "\(bu" 4 random_item_delete_index .IP "\(bu" 4 list_diff_hash .IP "\(bu" 4 list_diff_inboth .IP "\(bu" 4 list_diff_infirst .IP "\(bu" 4 list_diff_insecond .Sp These were all suggested by Dan Muey. .IP "\(bu" 4 listify .Sp Always return a flat list when either a simple scalar value was passed or an array-reference. Suggested by Mark Summersault. .SH "SEE ALSO" .IX Header "SEE ALSO" List::Util, List::AllUtils, List::UtilsBy .SH "HISTORICAL COPYRIGHT" .IX Header "HISTORICAL COPYRIGHT" Some parts copyright 2011 Aaron Crane. .PP Copyright 2004 \- 2010 by Tassilo von Parseval .PP Copyright 2013 \- 2015 by Jens Rehsack .SH "SUPPORT" .IX Header "SUPPORT" Bugs may be submitted at . .SH "SOURCE" .IX Header "SOURCE" The source code repository for List-SomeUtils 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 "AUTHORS" .IX Header "AUTHORS" .IP "\(bu" 4 Tassilo von Parseval .IP "\(bu" 4 Adam Kennedy .IP "\(bu" 4 Jens Rehsack .IP "\(bu" 4 Dave Rolsky .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Aaron Crane .IP "\(bu" 4 BackPan .IP "\(bu" 4 bay\-max1 <34803732+bay\-max1@users.noreply.github.com> .IP "\(bu" 4 Brad Forschinger .IP "\(bu" 4 David Golden .IP "\(bu" 4 jddurand .IP "\(bu" 4 Jens Rehsack .IP "\(bu" 4 J.R. Mash .IP "\(bu" 4 Karen Etheridge .IP "\(bu" 4 Ricardo Signes .IP "\(bu" 4 Toby Inkster .IP "\(bu" 4 Tokuhiro Matsuno .IP "\(bu" 4 Tom Wyant .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2022 by Dave Rolsky . .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .PP The full text of the license can be found in the \&\fI\s-1LICENSE\s0\fR file included with this distribution.